Replace Nan with blank in a TABLE

21 ビュー (過去 30 日間)
Anna Mascaretti
Anna Mascaretti 2021 年 2 月 10 日
編集済み: Walter Roberson 2021 年 2 月 10 日
Hello!
I have a table thant contains strings, numbers and NaNs and I have to replace the Nans with blanks.
How can I do?
Thank you

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 2 月 10 日
編集済み: Walter Roberson 2021 年 2 月 10 日
You need to iterate over all of the variables, find the ones that are numeric, and if the variable contains nan, then replace the variable with the formatted content of the numeric value, putting in blanks where-ever the nan were.
table() were not designed for presentation purposes, no Mathworks-provided function for this purpose.
YourTable = table([1;2;3;nan;5], {'a';'b';'c';'d';'e'}, [nan;12;13;14;15], rand(5,1))
YourTable = 5x4 table
Var1 Var2 Var3 Var4 ____ _____ ____ _______ 1 {'a'} NaN 0.82593 2 {'b'} 12 0.53086 3 {'c'} 13 0.94395 NaN {'d'} 14 0.34328 5 {'e'} 15 0.13109
YourTable = convertvars(YourTable, @isnumeric, @nanblank)
YourTable = 5x4 table
Var1 Var2 Var3 Var4 ____ _____ ____ _______ 1 {'a'} 0.82593 2 {'b'} 12 0.53086 3 {'c'} 13 0.94395 {'d'} 14 0.34328 5 {'e'} 15 0.13109
function output = nanblank(values)
mask = isnan(values);
if nnz(mask)
output = string(values);
output(mask) = "";
output = char(output);
else
output = values;
end
end

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by