MATLAB: How to write the NaN values in the matrix to Excel using XLSWRITE

MATLABnanxlswrite

I have a matrix that contains some NaNs. When I try to write that matrix to an Excel file using XLSREAD, all the NaN values show up as blank cells in Excel. I want NaN values to appear instead.

Best Answer

Excel does not know how to interpret the NaNs in the middle of a Numeric Matrix and hence treats them as blank spaces.
To write NaNs to Excel, they must be explicitly written as strings like 'NaN'. To achieve the same, you can convert your data matrix to a cell and replace all NaNs with 'NaN' before writing to Excel as shown below:
% A is the Data Matrix containing NaNs
B = num2cell(A);
B(isnan(A)) ={'NaN'};
xlswrite(filename,B);