How to convert comma separated column to table?

10 vues (au cours des 30 derniers jours)
Student1234
Student1234 le 13 Oct 2017
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:

Réponses (5)

Peter Perkins
Peter Perkins le 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.

KSSV
KSSV le 13 Oct 2017
readtable gives you a table...use load.
  2 commentaires
Student1234
Student1234 le 13 Oct 2017
Modifié(e) : Student1234 le 13 Oct 2017
Using:
if true
T = load(filename,'Delimiter','comma','Headliner',31)
end
But than I get the error:
if true
Error using load
Argument must contain a character vector.
I have a .dat file with first some text. After line 31 I have the data
Student1234
Student1234 le 13 Oct 2017
I think it is also the problem that there are empty values as well..

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 13 Oct 2017
T = readtable(filename,'Delimiter','comma', 'readvariable', false);
  4 commentaires
Student1234
Student1234 le 13 Oct 2017
No doesn't change much, but the thing I want to get rid off is that all my values are in one column..
Walter Roberson
Walter Roberson le 13 Oct 2017
Please attach a sample table for us to test with. You could restrict it to the first 5 or so lines.

Connectez-vous pour commenter.


Student1234
Student1234 le 13 Oct 2017
Modifié(e) : Walter Roberson le 13 Oct 2017
Like a .dat file like this:
Here a lot of text which is not needed
[Data]
1.1,0,,3,4,5
1,2.1,,1,2,3
2,3,,3,1,2.1
5,1,,3,2,1
So when I want to process this, I just get a table with one column instead of a table with 6 columns
  4 commentaires
Student1234
Student1234 le 13 Oct 2017
actually this are the kind of files (but now in txt file)
Walter Roberson
Walter Roberson le 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)

Connectez-vous pour commenter.


Student1234
Student1234 le 13 Oct 2017
Thanks for all the feedback but I think I will just use an other program to fix this problem.. I can not get a normal table out of this file.. Only the one column with variables separated by commas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by