Having difficulty reading in a file with csvread()
7 ビュー (過去 30 日間)
古いコメントを表示
Hi All,
I have a text file which looks like this:
Date,Col1,Col2
2012/06/06 11:40:12,1.01849,1.01881
2012/06/06 11:40:13,1.0185,1.01881
etc
I want to read this into a matrix like this:
[2012 06 06 11 40 12 1.01849 1.01881;
2012 06 06 11 40 12 1.01850 1.01881
etc
]
I can't seem to do this with csvread or textscan.
Any ideas?
0 件のコメント
採用された回答
dpb
2014 年 3 月 12 日
csvread can't because it's not comma-delimited all fields so that's a non-starter
Air code--
fmt='%d/d/d d:d:d,%f,%f';
d=cell2mat(textscan(fid,fmt,'headerlines',1,'delimiter',''));
Caveat: As said, untested and while I think the 'delimiter','' and explicit delimiter matching will work, the C input parsing often throws a curveball. Anyway, that's where I'd start.
0 件のコメント
その他の回答 (2 件)
Stewart Charles
2014 年 3 月 25 日
1 件のコメント
dpb
2014 年 3 月 25 日
編集済み: dpb
2014 年 3 月 25 日
Yeah, don't know why didn't get all the % signs in there--I guess 'cuz I'm a Fortran guy at heart and detest the C format strings, maybe... :)
I guess I hadn't ever noticed that problem before -- but it's simple enough to work around as one doesn't have to use %d for the integer values...
>> type stew.txt
Date,Col1,Col2
2012/06/06 11:40:12,1.01849,1.01881
2012/06/06 11:40:13,1.0185,1.01881
>> d=cell2mat(textscan(fid,'%f/%f/%f %f:%f:%f,%f, %f', ...
'headerlines',1,'collectoutput',1));
>> num2str(d,'%.4f')
ans =
2012.0000 6.0000 6.0000 11.0000 40.0000 12.0000 1.0185 1.0188
2012.0000 6.0000 6.0000 11.0000 40.0000 13.0000 1.0185 1.0188
>>
Image Analyst
2014 年 3 月 25 日
Wow. Complicated. Hopefully you have R2013b or later because by far the easiest way is to just simply create a table with readtable():
t = readtable('test.dat')
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!