开发者

textscan in MATLAB: read NULL value as NaN

开发者 https://www.devze.com 2023-03-19 18:56 出处:网络
I have a .txt file with the followin开发者_开发问答g data: sampleF.txt --> (tab delimited) MSFT200100

I have a .txt file with the followin开发者_开发问答g 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] } 


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消