Why can't MATLAB open this csv file properly?
Show older comments
I've got a .csv file containing data that I'd like to read into MATLAB. When I use csvread(filename, 1, 0) to tell it to skip the header line, I get this error:
Error using dlmread (line 138)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 1u) ==>
Error in csvread (line 47)
m=dlmread(filename, ',', r, c);
While troubleshooting, I think I've discovered part of the problem. Opening the .csv in notepad shows me this text for the first two lines:
Time(s),Absorbance (AU)
1.5,0.0681447982788086
All other entries look as I'd expect. However, opening the same file in MATLAB shows me this:
ÿþT i m e ( s ) A b s o r b a n c e ( A U )
All of the other entries are blank.
What is causing this issue, and how can I resolve it?
3 Comments
dpb
on 11 Feb 2016
You've got some non-ASCII characters in the file, apparently...
>> double('ÿþ')
ans =
255 254
>>
How best to resolve it depends largely on how the file was generated and whether you can go back and create it again without those issues.
Otherwise you're forced into trying to cleanup a bum file format and that's a quagmire of unknown depth and complexity depending on just what is in it and where and whether it's consistent between files and the phase of the moon and...
sadc
on 11 Feb 2016
Walter Roberson
on 11 Feb 2016
No need, see my code.
Accepted Answer
More Answers (1)
Walter Roberson
on 11 Feb 2016
The 'ÿþ' is the characteristic sign of UTF-16-LE ("little endian") encoding of a file.
Depending on your version of MATLAB, dlmread() might not be able to skip the text header.
If you open the file as a text file, MATLAB should be able to figure out the encoding based upon the byte order marks.
Try:
fid = fopen(filename, 'rt');
datacell = textscan(fid, '%f,%f', 'HeaderLines', 1, 'CollectOutput', 1);
fclose(fid);
data = datacell{1};
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!