在MATLAB中的textscan:读取NULL值为NaN [英] textscan in MATLAB: read NULL value as NaN

查看:2227
本文介绍了在MATLAB中的textscan:读取NULL值为NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.txt文件,包含以下数据:
$ b

sampleF.txt - >(制表符分隔)



I have a .txt file with the following data:

MSFT    200    100
APPL    10    NULL
AMZN    20    40

我需要使用 textscan 读取这些数据。我在阅读 NULL 数据时遇到问题。使用 treatasempty param,我可以将其读为0.但是我想把它读作 NaN 。请帮忙!谢谢!

I need to read this data using textscan. I am facing a problem while reading the NULL data. Using treatasempty param, I can read it as 0. But I want to read it as NaN. Please help! Thanks!

fName = '.....\sampleF.txt'
[fid, message] = fopen(fName) ;
if fid < 0, disp(message), else
    datatxt = textscan(fid, '%q %d %d', 'Delimiter', '\t','treatAsEmpty','NULL');
    datatxt = [ datatxt {1} num2cell(datatxt {2}) num2cell(datatxt {3})] ;
    fclose(fid) ; 
end

%datatxt = { 'MSFT' [200] [100] ; 'AAPL' [10] [NaN] ; 'AMZN' [20] [40] } 


推荐答案

问题在于类型 int32 不支持NaN值。而是把数字看作双打。即:

The problem is the fact that the type int32 does not support NaN values. Instead read the numbers as doubles. ie:

data = textscan(fid, '%s %f %f', 'Delimiter','\t', ...
           'treatAsEmpty','NULL', 'EmptyValue',NaN);

这篇关于在MATLAB中的textscan:读取NULL值为NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆