How to convert comma separated column to table?
10 views (last 30 days)
Show older comments
Hello,
I have a column with multiple values seperated by commas. Can I convert every value to a column, so I get a nice table?
This is my file:
if true
filename = fullfile('file.dat');
T = readtable(filename,'Delimiter','comma');
end
But I get a table 93x1 with multiple values in the column separated by commas:
0 Comments
Answers (5)
Peter Perkins
on 13 Oct 2017
The main problem is your file has a partial last line. Also you'll need to skip the two header lines.
In a recent version of MATLAB, all that is handled automatically by using detectImportOptions:
>> impopts = detectImportOptions('sample1.txt')
impopts =
DelimitedTextImportOptions with properties:
Format Properties:
Delimiter: {','}
Whitespace: '\b\t '
LineEnding: {'\n' '\r' '\r\n'}
CommentStyle: {}
ConsecutiveDelimitersRule: 'split'
LeadingDelimitersRule: 'keep'
EmptyLineRule: 'skip'
Encoding: 'ISO-8859-1'
Replacement Properties:
MissingRule: 'fill'
ImportErrorRule: 'fill'
ExtraColumnsRule: 'addvars'
Variable Import Properties: Set types by name using setvartype
VariableNames: {'Var1', 'Var2', 'Var3' ... and 19 more}
VariableTypes: {'char', 'double', 'double' ... and 19 more}
SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 19 more}
VariableOptions: Show all 22 VariableOptions
Access VariableOptions sub-properties using setvaropts/getvaropts
Location Properties:
DataLines: [4 Inf]
VariableNamesLine: 0
RowNamesColumn: 0
VariableUnitsLine: 0
VariableDescriptionsLine: 0
To display a preview of the table, use preview
>> t = readtable('sample1.txt', impopts)
t =
4×23 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 ExtraVar1
____ __________ _____ ______ _______ ____ _________ ______ ____ _____ _________ ______ _____ _____ __________ _____ __________ _____ _____ ______ _____ ______ _________
'' 2.3711e+07 69985 67.014 -9371.4 0 0.0047674 99.355 '' '' 0.0075378 95.881 '' '' 6.1332e-05 '' 3.3819e-05 '' 25 4.7674 '' 7.5378 ''
'' 2.3711e+07 69985 66.994 -8410.3 0 0.0047928 99.902 '' '' 0.007496 99.902 '' '' 2.1143e-05 '' 3.7657e-05 '' 25 4.7928 '' 7.496 ''
'' 2.3711e+07 69985 67.019 -7509.6 0 0.0048171 99.902 '' '' 0.0074728 99.902 '' '' 1.4552e-05 '' 5.3002e-05 '' 25 4.8171 '' 7.4728 ''
'' 2.3711e+07 69985 67.025 NaN NaN NaN NaN '' '' NaN NaN '' '' NaN '' NaN '' NaN NaN '' NaN ''
In an earlier version, get rid of the last partial data line, and skip the two header lines by passing 'HeaderLines',2 into readtable.
0 Comments
Walter Roberson
on 13 Oct 2017
T = readtable(filename,'Delimiter','comma', 'readvariable', false);
4 Comments
Walter Roberson
on 13 Oct 2017
Please attach a sample table for us to test with. You could restrict it to the first 5 or so lines.
Student1234
on 13 Oct 2017
Edited: Walter Roberson
on 13 Oct 2017
4 Comments
Walter Roberson
on 13 Oct 2017
Edit that sample1.txt to remove the last line (which has only 4 fields). Then,
T = readtable('sample1.txt', 'HeaderLines', 3, 'readvariablenames', false)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!