octavetextscan

GNU Octave: reading data with NA values using textscan(...)


I need to read data from an ASCII file where missing values are given as NA. Using textscan(...) does not seem to work, because textscan(...) seems to stop reading/parsing at the first occurrence of NA.

Here's a simple demonstration of the issue:

x = textscan ( "1 ; 2 ; 3\n4 ; NA ; 6" , '%d %d %d' , 'Delimiter' , ';' , 'ReturnOnError' , false )
error: textscan: Read error in field 2 of row 2

I have also tried to tell textscan(...) to interpret NA as "empty value", but no luck:

x = textscan ( "1 ; 2 ; 3\n4 ; NA ; 6" , '%d %d %d' , 'Delimiter' , ';' , 'TreatAsEmpty' , 'NA' , 'ReturnOnError' , false )
error: textscan: Read error in field 2 of row 2

Can someone explain what's going on, or how to make this work?

Note that is just a simplified example to illustrate the problem. The format of the data in my files is a bit more complex, and I really depend on textscan(...) to parse it; I don't think I can easily do it without textscan(...).

(I am running Octave 4.2.1.)


Solution

  • NA is defined for floating point numbers so you should use '%f' conversion specifier instead of '%d'.

    x = textscan ( "1 ; 2 ; 3\n4 ; NA ; 6" , '%f %f %f' ,
        'Delimiter' , ';' , 'ReturnOnError' , false )