MATLAB Answers

Vincent Odongo

Error using textread to read data containing "INF" or "NaN" with float data

Asked by Vincent Odongo
on 26 Jul 2013

I have a file with data showing date,time, and floating point data downloaded from an instrument. Some instances of the floating point data were recorded as "INF" meaning the data record was skipped or erroneous. See the sample data below:

"2012-02-15 06:34:00",965,0,1.367,-61.03,-4.838,8.64,281.8,-1.367,-56.2,"INF",-61.03,-3.471,-57.56,296.5,352.7
"2012-02-15 06:35:00",966,0.946,1.802,-60.79,-4.656,8.66,281.8,-0.856,-56.14,1.904,-59.85,-2.854,-56.99,296.8,352.9
"2012-02-15 06:36:00",967,1.325,1.926,-60.82,-5.14,8.67,281.8,-0.602,-55.68,1.454,-59.5,-3.214,-56.29,296.8,352.5

I have tried to read the data using the following line:

 [d1,counter,SR01Up,SR01Dn,IR01Up,IR01Dn,NR01TC,NR01TK,NetRs,NetRl,Albedo,UpTot,DnTot,NetTot,IR01UpCo,IR01DnCo] = textread([direct filename], '%22c %u %f %f %f %f %f %f %f %f %f %f %f %f %f %f','headerlines',4,'delimiter',',');

When I do, I get the following error;

Error using dataread
Trouble reading floating point number from file (row 966, field 11) ==> "INF",-61.03,-3.471,-57.56,2
Error in textread (line 176)
Error in CR3000dataproc2 (line 89)
      = textread([direct filename], '%22c %u %f %f %f %f %f %f %f %f %f %f %f %f %f %f 

Kindly help resolve this...




No products are associated with this question.

4 Answers

Answer by Matt Kindig
on 26 Jul 2013

Try using textscan() with the 'TreatAsEmpty' option:

output = textscan([direct filename], '%22c %u %f %f %f %f %f %f %f %f %f %f %f %f %f %f','delimiter',',', 'EmptyValue', NaN,'TreatAsEmpty', '"INF"');
d1= output{1};
counter = output{2};  %etc.

All the "INF" should now be converted to NaN.

  1 Comment

on 26 Jul 2013

NB the character format "%22c" will end up returning the trailing comma as well as the two surrounding '"' marks on the date field as the actual date field is only 19 characters wide w/ leading and trailing double quotes to surround an embedded space in the string.


fmt=['"%19c" %u' repmat('%f',1,14)];

will return just the actual date/time string w/o additional post-processing to use it.

With 'collectoutput', 1 the first record is returned w/ the above fmt as

    '2012-02-15 06:34:00'    [965]    [1x14 double]

instead of as

    '"2012-02-15 06:34:00",'    [965]    [1x14 double]

Just a nit, but eliminates one additional step later...

Answer by dpb
on 26 Jul 2013
Edited by dpb
on 26 Jul 2013

Here's a case where the "red-haired stepchild" status of textread hurts--it can't accept a custom 'TreatAsEmpty' string property. Use textscan instead.

Unfortunately, textscan has its own quirks; the most irritating of which is it can't return anything except a cell array... :(

>> l='"2012-02-15 06:34:00",965,0,1.367,-61.03,-4.838,8.64,281.8,-1.367,-56.2,"INF",-61.03,-3.471,-57.56,296.5,352.7 ';
>> fmt=['%s %u' repmat('%f',1,14)];
>> textscan(l, fmt,'delimiter',',','treatasempty',{'"INF"'},'collectoutput',1)
ans = 
  {1x1 cell}    [965]    [1x14 double]


Answer by Vincent Odongo
on 27 Jul 2013

Sorry guys, Doesn't seem to work for me...

I used your suggestions as follows...

[d1,counter,SR01Up,SR01Dn,IR01Up,IR01Dn,NR01TC,NR01TK,NetRs,NetRl,Albedo,UpTot,DnTot,NetTot,IR01UpCo,IR01DnCo] = textscan([direct filename], '%22c %u %f %f %f %f %f %f %f %f %f %f %f %f %f %f','headerlines',4,'delimiter',',','EmptyValue', NaN,'TreatAsEmpty', '"INF"');

On running the script I get the following error; Error using textscan Too many output arguments.

The same errors appears when I use; [d1,counter,SR01Up,SR01Dn,IR01Up,IR01Dn,NR01TC,NR01TK,NetRs,NetRl,Albedo,UpTot,DnTot,NetTot,IR01UpCo,IR01DnCo] = textscan([direct filename], '%22c %u %f %f %f %f %f %f %f %f %f %f %f %f %f %f','headerlines',4,'delimiter',',','treatasempty',{'"INF"'},'collectoutput',1)

  1 Comment


    cac = textscan([direct filename], '%22c %u %f %f %f %f %f %f %f %f %f %f %f 
    %f %f %f','headerlines',4,'delimiter',',','EmptyValue', NaN,'TreatAsEmpty', 

and markup your text

Answer by dpb
on 27 Jul 2013
Edited by dpb
on 28 Jul 2013

[d1,counter,...,IR01DnCo] = textscan([direct filename], ...

On running the script I get the following error; Error using textscan Too many output arguments.

Well, yes, as per the doc, textscan returns ONLY a cell output.

doc textscan

As I showed earlier for the typical line

 l='"2012-02-15 06:34:00",965,
         -61.03,-3.471,-57.56,296.5,352.7 ';

and the format string

fmt=['"%19c" %u' repmat('%f',1,14)];
C=textscan(l, fmt,'delimiter',',','treatasempty',{'"INF"'},'collectoutput',1)
C = 
  '2012-02-15 06:34:00'    [965]    [1x14 double]

Also as noted previously there's a problem w/ an earlier respondent's format of '%22c' for the date/time string in doesn't character-match the leading/trailing '"' and picks up the delimiter comma as well as the actual data in the string is only 19 characters long--it's wrapped in " for the embedded spaces (21 char's total). The length of 22 then gets the delimiter, too.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!