MATLAB: Won’t convert NaNs in cell array to zeros

fprintfnan to zerospreadsheet importxls to txtxlsread

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

Best Answer

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