简体   繁体   English

GNU Octave:使用textscan(…)读取具有NA值的数据

[英]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. 我需要从ASCII文件中读取数据,其中缺失值以NA给出。 Using textscan(...) does not seem to work, because textscan(...) seems to stop reading/parsing at the first occurrence of NA. 使用textscan(...)似乎不起作用,因为textscan(...)在第一次出现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: 我也试图告诉textscan(...)将NA解释为“空值”,但是没有运气:

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; 我文件中数据的格式有点复杂,我真的依赖于textscan(...)来解析它; I don't think I can easily do it without textscan(...). 我认为没有textscan(...)就无法轻松做到这一点。

(I am running Octave 4.2.1.) (我正在运行Octave 4.2.1。)

NA is defined for floating point numbers so you should use '%f' conversion specifier instead of '%d' . NA是为浮点数定义的,因此您应该使用'%f'转换说明符而不是'%d'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM