import problem with CSV file
9 views (last 30 days)
Show older comments
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);
0 Comments
Answers (1)
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
Noam Greenboim
on 21 May 2020
For removing BOM, you can easily use this function (see link below):
BOM('test.csv','')
See Also
Categories
Find more on String Parsing 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!