import problem with CSV file

조회 수: 16 (최근 30일)
au198295
au198295 2018년 3월 16일
댓글: Noam Greenboim 2020년 5월 21일
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);

답변 (1개)

Jan
Jan 2018년 3월 16일
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
au198295
au198295 2018년 3월 16일
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.
Noam Greenboim
Noam Greenboim 2020년 5월 21일
For removing BOM, you can easily use this function (see link below):
BOM('test.csv','')

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by