Textscan problem, empty values and NaN

19 views (last 30 days)
Stefán
Stefán on 5 Jun 2014
Edited: dpb on 5 Jun 2014
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.

Answers (1)

dpb
dpb on 5 Jun 2014
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 Comments
Stefán
Stefán on 5 Jun 2014
Thank for the reply. There is a space (not tab) between the date and the time so that's why I only have one string format. The data I am trying to read is built similar as in my/your example.
It starts with 6 %f variables and then 11 %f variables with empty space (double tab) after every 12. So overall 6+(12*(11+1))=150.
So my format is therefore
format = ['%s' repmat('%f', [1 nNumberCols])];
So when I use textscan with a tab delimeter the result is
ans={X x 2 cell} [X x 150 double] and ans(1) is as I described above.
Is textscan having trouble reading the space or?
dpb
dpb on 5 Jun 2014
Edited: dpb on 5 Jun 2014
>> 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);

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!