How can I write the NaN values in the matrix to Excel using XLSWRITE?

43 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Feb 2018
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);

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!