问题
I am trying to read a tab delimited txt file in MatLab. The file has columns composed of numbers, text, dates, datetimes, everything you can think of. Some of the columns have very long sentences in them, with commas and everything.
it exceeds the row limit of excel (i have about 1.5 million rows) so I can not convert it to a CSV or an XLSX file.
I have tried the following:
tableDataEDM = tdfread(pathDataEDM,'\t');
I get back 'need the statistics and machine learning toolbox' I dont have it
tableDataEDM = dlmread(pathDataEDM,'\t');
Mismatch between file and format character vector. I get back 'Trouble reading 'Numeric' field from file'
this is because the file has text and numbers and dates and everything else. dlmread likes numeric data i guess
tableDataEDM = readtable(pathDataEDM,'Delimiter','\t','ReadVariableNames',true);
I get back:
Error using readtable (line 197)
Reading failed at line 6. All lines of a text file must have the same number of delimiters. Line 6 has 10
delimiters, while preceding lines have 32.
Note: readtable detected the following parameters:
'HeaderLines', 0, 'Format', '%q%q%q%q%D%D%D%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%D%D%q%q%q%q%q%q%q%q%q'
Error in Edm_FinderComp_CrossOver (line 33)
tableDataEDM = readtable(pathDataEDM,'Delimiter','\t','ReadVariableNames',false);
I am not sure why it says this though. I can import this data into python very easily. Is there something I am missing to try and get this into matlab?
any help on import tab delimited txt files is appreciated. I have not tried text scan because it looks painful.
回答1:
You can use textscan. This will put each column in a separate cell in tableDataEDM
. Assuming that for each row, you have an int
\t int
\t string
:
tableDataEDM = textscan(fopen(pathDataEDM),'%d %d %s')
The last argument, '%d %d %s'
you should change to match your formatting.
回答2:
Have you tried just using the import wizard and changing the delimiter to tabs? Just drag the file into the workspace window and the wizard will appear.
来源:https://stackoverflow.com/questions/43506729/read-a-tab-delimited-txt-file-into-matlab