Trouble reading csv file
68 次查看(过去 30 天)
显示 更早的评论
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)
Opening the file in Excel and clicking "Save as". I have attached a sample. Thanks.
2 个评论
per isakson
2018-8-3
"opening the the file in and clicking save as" in what program?
Could you upload (with the paperclip button) a sample file?
per isakson
2018-8-3
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.
采纳的回答
per isakson
2018-8-3
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 );
and
>> 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
7 个评论
Arshey Dhangekar
2021-6-16
I have two csv file WT and both have fixed have 52 header. I tried to use below command to read however headers are showing as Var1,Var2..to Var52 instead of Store no,Date,Time....till 52th header. How can I fix the header problem with importing all the datas from those files and without doing any edit in excel file? Using opts = detectImportOptions()
table = readtable('WT_201120.csv');
Walter Roberson
2021-6-18
As discussed in https://www.mathworks.com/matlabcentral/answers/858175-how-can-i-add-path-in-matlab-to-read-files-csv-and-excel-files-from-specific-folder-and-read-the-dat#answer_726490 your list of variables is separated by semi-colon instead of comma.
更多回答(2 个)
Walter Roberson
2018-8-3
(Note: that version does not have my enhancements to deal with duration objects)
2 个评论
Walter Roberson
2018-8-3
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);
Alec Jacobson
2020-3-16
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 个评论
Walter Roberson
2020-3-18
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.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!