import problem with CSV file

9 views (last 30 days)
au198295
au198295 on 16 Mar 2018
Commented: Noam Greenboim on 21 May 2020
I am having trouble importing CSV files that are output by a spectrometer system I am using. I can't figure out what is wrong with the file but routines I had written awhile ago using the commandline 'csvread' function. I also tried the commandline 'importdata' function which sort of works but doesn't read the file properly either. It appears like both are encountering some odd characters, "", at the beginning of the file. I have opened up several of these trouble CSV files in Excel, Word, Wordpad and Notepad (as CSV files or after changing the extension to TXT) and don't see any problems in any of those programs. Additionally, if I click on the "Import Data" button in the Home tab of matlab and select one of these files it seems to have no issues and interprets the file as expected. Below is the output I get using the commandline 'importdata' and 'csvread' read functions where the odd characters can be seen. I've attached one of the CSV files as an example. I am running Win 7 and Matlab R2017a. Any help would be appreciated.
Thanks, Aaron
>> data = importdata('test.csv');
>> data
data =
struct with fields:
data: [1340×3 double]
textdata: {1340×1 cell}
rowheaders: {1340×1 cell}
>> data.rowheaders{1}
ans =
'1'
>> data = csvread('test.csv');
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==>
1,523.73748068959833,0,1\n
Error in csvread (line 48)
m=dlmread(filename, ',', r, c);

Answers (1)

Jan
Jan on 16 Mar 2018
The "" is a "BOM", a Byte Order Mark. This is required for UTF files. When I open the attached file, I do not see such a BOM and therefore I cannot reproduce your problem. The solution is to remove it by recreating the files. You can import the contents by fileread or fread, remove the BOM and write the data back to the disk.
By the way, if I ask an internet search engine for "", I get equivalent answers. It is recommended to search before asking.
  2 Comments
au198295
au198295 on 16 Mar 2018
Thanks for the prompt response. Sorry for the question without thoroughly searching. I would still have been puzzled as to why Matlab couldn't handle this, particularly because I had successfully imported into Matlab CSV files output by the same spectrometer software in the past. I just now tried re-downloading from here the CSV file I uploaded, in case the BOM got stripped out in transit somehow, and I get the same result when trying to read it in. Not sure why it isn't reproducible on your end. Anyway, I can implement something to rewrite the files to fix it. Thanks for the help.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!