How to export "Nan" and "inf" in a table from MATLAB to EXCEL as text?

34 views (last 30 days)
I created a code that generates a table "R" that has a column called "number" which has values including inf and NaN. I'm exporting this table into EXCEL. However, NaN shows as blank cell and inf shows as 65535. Is it possible to convert Nan and inf as text to EXCEL?
I was trying to add a for loop that checks for any inf and NaN in table and convert them to a string but I'm not able to do it correctly. Here is my attempt:
for y=1:10
R.number(isinf(R.number))= ;
R.number(isnan(R.number))= ;
end
writetable(R,'r.xlsx','Sheet',1);
Any help is appreciated!

Accepted Answer

Walter Roberson
Walter Roberson on 15 Mar 2023
base = tempname;
cname = base + ".csv";
xname = base + ".xlsx";
A = [1; 2; 3; nan; inf];
writematrix(A, cname)
dbtype(cname)
1 1 2 2 3 3 4 NaN 5 Inf
B = readcell(cname)
B = 5×1 cell array
{[ 1]} {[ 2]} {[ 3]} {[NaN]} {[Inf]}
writematrix(A, xname)
C = readcell(xname)
C = 5×1 cell array
{[ 1]} {[ 2]} {[ 3]} {1×1 missing} {[ 65535]}
masknan = isnan(A);
maskinf = A == inf;
D = num2cell(A);
D(masknan) = {'NaN'};
D(maskinf) = {'Inf'};
writecell(D, xname)
E = readcell(xname)
E = 5×1 cell array
{[ 1]} {[ 2]} {[ 3]} {'NaN'} {'Inf'}

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!