Won't convert NaNs in cell array to zeros

The function display all the spreadsheet information in the proper formatting, but it will not change the NaN cells to zeros.
function [data] = importxls(workbookfile)
[~,~,data_MRN] = xlsread(workbookfile);
[~,sheetnames] = xlsfinfo(workbookfile);
m = size(sheetnames,2);
data = cell(1,m);
for k = 4:m;
sheet = char(sheetnames(1,k));
[~,~,data{k}] = xlsread(workbookfile, sheet);
[r,c] = size(data{k});
fid = fopen('importxls.txt','a');
for i = 2:r
fprintf(fid, '%-10.11s',num2str(data_MRN{2,2}));
fprintf(fid, '%-10.11s',num2str(k-3));
fprintf(fid, '%-10.11s',num2str(0));
for j = 1:c
if strcmp('NaN',data{k}{i,j}) == 1
data{k}{i,j} = '0';
elseif iscellstr(data{k}{i,j}) == 1
data{k}{i,j} = char(data{k}{i,j});
elseif isa(data{k}{i,j},'char') == 0
data{k}{i,j} = num2str(data{k}{i,j});
end
fprintf(fid, '%-10.11s',data{k}{i,j});
end
fprintf(fid, '\n');
end
end
fclose(fid);
end

3 件のコメント

Honglei Chen
Honglei Chen 2012 年 10 月 16 日
Edited to format the code
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 16 日
what is the question?
Ryan
Ryan 2012 年 10 月 17 日
when the xls is read, the empty cells are converted to NaN. The first if statement was written to convert these NaNs to '0' so it would be written in the txt file as a 0. The NaNs remain after the code is run, and I have no idea why.

サインインしてコメントする。

 採用された回答

Honglei Chen
Honglei Chen 2012 年 10 月 16 日

1 投票

You should use isnan to test whether a number is NaN or not.

2 件のコメント

Matt Fig
Matt Fig 2012 年 10 月 16 日
Yes, NaN is not a string. So this line:
strcmp('NaN',data{k}{i,j})
Does not do what you want. Use:
isnan(data{k}{i,j})
Or better yet, drop the loop and use logical indexing.
Ryan
Ryan 2012 年 10 月 17 日
Thanks for the help. I replaced the strcmp() with isnan(). I play around with indexing later.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

質問済み:

2012 年 10 月 16 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by