cannot read .csv

6 ビュー (過去 30 日間)
j shen
j shen 2018 年 11 月 2 日
編集済み: Walter Roberson 2018 年 11 月 4 日
I am trying to read a .csv file and tried csvread() and textscan(), none of them is worked.
When I use csv(fname) it said:
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==>
Y,D,1,stim/peach.bmp,PER,S,L,29972,4,601\n
When I use textscan(fname) it said:
Error using textscan
Not enough input arguments.
  2 件のコメント
Stephen23
Stephen23 2018 年 11 月 2 日
編集済み: Stephen23 2018 年 11 月 2 日
textscan reads your file perfectly:
opt = {'Delimiter',','};
fmt = '%s%s%f%s%s%s%s%f%f%f';
[fid,msg] = fopen('y01-1.csv','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
And some of the imported data:
>> C{1}(1:8)
ans =
'Y'
'Y'
'Y'
'Y'
'Y'
'Y'
'Y'
'Y'
>> C{2}(1:8)
ans =
'A'
'A'
'D'
'A'
'A'
'A'
'A'
'A'
>> C{3}(1:8)
ans =
3
3
1
3
3
3
3
3
... etc.
How did you try to use it?
j shen
j shen 2018 年 11 月 2 日
this one works better, thank you!

サインインしてコメントする。

採用された回答

KSSV
KSSV 2018 年 11 月 2 日
編集済み: KSSV 2018 年 11 月 2 日
[num,txt,raw] = xlsread('y01-1.csv') ;
The better would be:
T = readtable('y01-1.csv')
  1 件のコメント
j shen
j shen 2018 年 11 月 2 日
Thank you!

サインインしてコメントする。

その他の回答 (2 件)

madhan ravi
madhan ravi 2018 年 11 月 2 日
fid=fopen('y01-01.csv','r')
f = textscan(fid,'%s','delimiter',',')
fclose(fid)
or use

Walter Roberson
Walter Roberson 2018 年 11 月 2 日
Use
readtable(fname, 'headerlines', 0)
csvread cannot be used to read files that contain text after a wanted numeric value. dlmread can be but only in a useless way.
textscan requires that you first fopen the file, and pass the file identifier and a format string into textscan.

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by