Textscan problem, empty values and NaN
조회 수: 13 (최근 30일)
이전 댓글 표시
I have the following data file
example.txt
and I use
Heading
Heading
Explanation
2014/02/20 12:00 4.80 NaN * 8.81 21.28 * 0.78 *
2014/02/21 12:00 5.70 NaN * 7.77 19.98 * 0.88 *
The * shows empty spaces or double tab if you will
nNumberCols = 8;
format = ['%s' repmat('%f', [1 nNumberCols])];
fid = fopen(filename, 'r');
c = textscan(fid, format, ... 'Delimiter','t', 'CollectOutput',1,... 'HeaderLines',3,'EmptyValue', Inf);
fclose(fid);
But I get a 1x2 cell with
c{1,1} = 2014/02/20 12:004.80NaN8.8121.280.78
etc
Is there anyway to solve this?
Also importdata doesn't work.
Thanks in advance.
댓글 수: 0
답변 (1개)
dpb
2014년 6월 5일
Fixing the format statement should do it...
>> t=char(9); % tab char to build a sample string
>> l=['2014/02/20' t '12:00' t '4.80' t 'NaN' t t '8.81' t '21.28' t t '0.78' t t];
>> nNumberCols = 8;
>> format = ['%s %s' repmat('%f', [1 nNumberCols])]; % don't forget the time field this time...
>> textscan(l,format,'collectoutput',1,'delimiter','\t')
ans =
{1x2 cell} [1x8 double]
>> ans{:}
ans =
'2014/02/20' '12:00'
ans =
4.8000 NaN NaN 8.8100 21.2800 NaN 0.7800 NaN
>>
댓글 수: 2
dpb
2014년 6월 5일
편집: dpb
2014년 6월 5일
>> l=['2014/02/20 12:00' t '4.80' t 'NaN' t t '8.81' t '21.28' t t '0.78' t t];
>> format = ['%s' repmat('%f', [1 nNumberCols])];
>> textscan(l,format,'collectoutput',1,'delimiter','\t','emptyvalue',inf)
ans =
{1x1 cell} [1x8 double]
>> ans{:}
ans =
'2014/02/20 12:00'
ans =
4.8000 NaN Inf 8.8100 21.2800 Inf 0.7800 Inf
>>
I can only presume the actual format of the record isn't quite what you think. The sample isn't the real thing but the missing values show up ok as well as the tab-delimited string.
Double-check you have tabs everywhere you think you do and the right count. The run-on of the character field thru the numeric data looks to me like there isn't actually a tab delimiter in the file. How did you write it, precisely?
Just to confirm no problem whether on disk or in memory, I did the following test--
>> fid=fopen('stef.dat','w');
>> fprintf(fid,'%s\n',l);
>> fclose(fid);
>> fid=fopen('stef.dat','r');
>> textscan(fid,format,'collectoutput',1,'delimiter','\t','emptyvalue',inf)
ans =
{1x1 cell} [1x8 double]
>> ans{:}
ans =
'2014/02/20 12:00'
ans =
4.8000 NaN Inf 8.8100 21.2800 Inf 0.7800 Inf
>> fid=fclose(fid);
참고 항목
카테고리
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!