MATLAB 中的 textscan:将 NULL 值读取为 NaN
我有一个包含以下数据的 .txt 文件:
sampleF.txt --> (制表符分隔)
MSFT 200 100
APPL 10 NULL
AMZN 20 40
我需要使用 textscan
读取此数据。我在读取 NULL 数据时遇到问题。使用 treatasempty
参数,我可以将其读取为 0。但我想将其读取为 NaN
。请帮忙!谢谢!
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] }
I have a .txt file with the following data:
sampleF.txt --> (tab delimited)
MSFT 200 100
APPL 10 NULL
AMZN 20 40
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] }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 int32 类型不支持 NaN 值。相反,将数字读取为双精度数。 IE:
The problem is the fact that the type
int32
does not support NaN values. Instead read the numbers as doubles. ie: