problem in importing data through textscan
1 回表示 (過去 30 日間)
古いコメントを表示
Hello everybody,
In fact, I have a text file with dat extension in which the data format is as follows:
Date Time CO
2009-02-25 18:39:01 165.3
I used the following code to import the data:
fid = fopen('CO_2009_m.dat','r');
data_f2 = textscan(fid,'%s %s %f', 'delimiter', ' ', 'collectoutput',1,'headerlines',1);
fclose(fid)
and I found where my CO concentration is two digits before decimal, for instance, 70.1, the output is as follows:
357.9
357.2
NaN
NaN
359.4
and even sometimes the data is mixed with the string as follows:
'2009-01-02' '19:30:00'
'4.6' ''
2009-01-02' '19:40:00'
'2009-01-02' '19:50:00'
'0.5' ''
Does any body have any idea how can I get rid of this situation importing the string and the data separately?
kind regards,
Rahman
1 件のコメント
per isakson
2012 年 8 月 22 日
Are you sure it is space delimited? Try and remove the spaces from the format specifier.
採用された回答
per isakson
2012 年 8 月 22 日
編集済み: per isakson
2012 年 8 月 22 日
Try this
fid = fopen('cssm.txt', 'r' );
data_f2 = textscan( fid,'%s%s%f' ...
, 'delimiter' , ' ' ...
, 'headerlines' , 1 ...
, 'MultipleDelimsAsOne' , true ...
, 'CollectOutput' , true );
fclose(fid)
.
and this
fid = fopen('cssm.txt', 'r' );
data_f2 = textscan( fid,'%s%s%f' ...
... , 'delimiter' , ' ' ...
, 'headerlines' , 1 ...
... , 'MultipleDelimsAsOne' , true ...
, 'CollectOutput' , true ...
);
fclose(fid)
.
Tested with R2012a, 64bit, Windows7.
Default for Delimiter is "white-space", which includes space, tab, etc.
Doc says: "... If you do not specify a delimiter, textscan interprets repeated white-space characters as a single delimiter."
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Import and Export についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!