MATLAB Answers

Trouble reading csv file

90 views (last 30 days)
My files are saved from the microscope as .csv, but there maybe a problem with the format because opening the the file in and clicking save as, it shows "save as type" as Unicode Text (*.txt).
Anyways, I am using the below
[nAme, PATHNAME, FILTERINDEX] = uigetfile('*.csv');
Dataset = csvread(nAme,1,0);
but I get the following error;
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number
1) ==> \n
Error in csvread (line 48)
m=dlmread(filename, ',', r, c);
However, when I resave the file and change it from Unicode text to CSV (comma delimited), the code runs. I have so many files and having to resave all is a huge task. Can some suggest what I can do? PS: I have tried reading it as a (*.txt) but the call up doesn't see the file as (*.txt)
EDIT: Ifechukwu Michaels's "Answer" and uploaded file moved here:
Opening the file in Excel and clicking "Save as". I have attached a sample. Thanks.
per isakson
per isakson on 3 Aug 2018
I've downloaded you sample file, 7000.csv. The editor, Notepad++, identifies the text encoding as UCS-2 LE BOM.
fid = fopen( '7000.csv', 'r', 'ieee-le', 'UCS-2 LE' );
cac = textscan( fid, '%f%f%f%f%f%f%f', 'Headerlines',1 ...
, 'CollectOutput',true, 'ReturnOnError',false );
fclose( fid );
returns the error
Error using textscan
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==>
1 - 4 2 9 . 1 1 2 7 6 9 9 7 . 2 8 9 9 0 . 0 0 0 0 - 7 4 . 0 4 3 2 \n
Error in Untitled (line 3)
cac = textscan( fid, '%f%f%f%f%f%f%f', 'Headerlines',1 ...
Yes, each digit is coded with two bytes.
Google "windows text file change encoding" returned this Changing source files encoding and some fun with PowerShell and more
Your answer to my comment should have been entered as a comment after my comment. Not as an answer to your own question. One reason is that some potential contributors will not look at a question on reading a text file, which has already received an answer.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 3 Aug 2018
This hack reads and parses your file
str = fileread( '7000.csv' );
iszero = double(str) == 0;
str(iszero) = [];
cac = textscan( str, '%f%f%f%f%f%f%f', 'Headerlines',1 ...
, 'CollectOutput',true, 'ReturnOnError',false );
>> cac{1}
ans =
1.0e+03 *
0.0010 -0.4291 6.9973 0 -0.0740 NaN NaN
0.0020 -0.4278 6.9973 0.0013 -0.0739 0 0.0068
0.0030 -0.4265 6.9973 0.0026 -0.0735 0 0.0163
0.0040 -0.4252 6.9973 0.0039 -0.0730 0 0.0194
0.0050 -0.4239 6.9973 0.0052 -0.0725 0 0.0213

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 3 Aug 2018
Walter Roberson
Walter Roberson on 3 Aug 2018
Datatable = csv2table(nAme);
Provided you have R2013b or later, this would return a table() object. The table variable names would be derived from the first line of the file, which could have the effect of modifying them, such as replacing spaces with underscores, or adding a leading character if the word begins with a digit. If you want to see the original version of the first line, then
[Datatable, ~, headers] = csv2table(nAme);

Sign in to comment.

Alec Jacobson
Alec Jacobson on 16 Mar 2020
I ran into this issue copy-pasting data in excel into a new sheet and saving as .csv.
Seems on mac, this maneuver triggers Excel to save as "CSV UTF-8 (Comma delimited) (.csv)" which Matlab (2019a at least) doesn't understand. Specifically there seems to be an initial problem character. I know, I know, UTF-8 has been around since 1993 and MATLAB doesn't support it.
Here's a work around. Save as in excel as "Comma Separated Values (.csv)". This will probably remove any unicode text so watch out. For me, I just had numbers so it was fine.
  1 Comment
Walter Roberson
Walter Roberson on 18 Mar 2020
MATLAB does not understand Byte Order Mark on UTF-8, which is something that the standards recommend against including (I do not understand their reasoning; my feeling is that it is better to include it.)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!