Reading data with NA values using textscan(...)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Reading data with NA values using textscan(...)

Matthias Brennwald
Hello

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", 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?

(I am running Octave 4.2.1)

Thanks
Matthias


-----------------------------------------
Join us March 12-15 at CERN near Geneva
Switzerland for OctConf 2018.  More info:
https://wiki.octave.org/OctConf_2018
-----------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: Reading data with NA values using textscan(...)

PhilipNienhuis
Matthias Brennwald wrote

> Hello
>
> 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", 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?
>
> (I am running Octave 4.2.1)

Just for comparison:
Matlab r2018a prerelease does this (for IMO an equivalent command):

>> x = textscan ( ['1 ; 2 ; 3' char(10) '4 ; NA ; 6'] , '%d %d %d' ,
>> 'Delimiter' , ';' , 'ReturnOnError' , false )
Error using textscan
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 2, field number 2) ==>
NA ; 6\n

Did you try in Matlab yourself?

Philip




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


-----------------------------------------
Join us March 12-15 at CERN near Geneva
Switzerland for OctConf 2018.  More info:
https://wiki.octave.org/OctConf_2018
-----------------------------------------