Trouble reading csv file
107 views (last 30 days)
Ifechukwu Michaels on 3 Aug 2018
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 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 );
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
More Answers (2)
Walter Roberson on 3 Aug 2018
(Note: that version does not have my enhancements to deal with duration objects)
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.