convert double NaN to string NaN

49 次查看(过去 30 天)
Hi
I have a cell array with NaN as shown in the image. How can I convert NaN to string NaN meaning instead of of seeing NaN in the array, I want to see "NaN".
I have tried this: TC(cellfun(@(x) any(isnan(x)),TC,'UniformOutput',false)) = {"NaN"}; but no success.
Any help is appreciated.

采纳的回答

KSSV
KSSV 2021-8-24
A = NaN(2,5) ;
C = arrayfun(@num2str,A,'UniformOutput',false)
C = 2×5 cell array
{'NaN'} {'NaN'} {'NaN'} {'NaN'} {'NaN'} {'NaN'} {'NaN'} {'NaN'} {'NaN'} {'NaN'}
  5 个评论
Daneisha Blair
Daneisha Blair 2021-8-24
Wait, how can I get it in string array instead of cell array, please?
Daneisha Blair
Daneisha Blair 2021-8-24
I got it :) . string(arrayfun(@num2str,A,'UniformOutput',false)).

请先登录,再进行评论。

更多回答(1 个)

darova
darova 2021-8-24
Try num2str
a = {nan 1 nan};
cellfun(@num2str,a,'UniformOutput',0)
ans = 1×2 cell array
{'NaN'} {'NaN'}
  1 个评论
Daneisha Blair
Daneisha Blair 2021-8-24
This didn't give me quite what I was looking for.
For clarification, TC is in the structure of:
TC = {["Mercury","Gemini", "Apollo"; ...
"Skylab", "Skylab B","ISS"], ...
["Mercury","Gemini";
"Skylab","Skylab B"]}
% Output array
TC = cellfun(@(x) nan(size(x,1),1) ,TC,'UniformOutput',false)
which created double NaN
However, I want it in string NaN, like this {'NaN'}
Hope this help.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by