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.

 Akzeptierte Antwort

KSSV
KSSV am 24 Aug. 2021

0 Stimmen

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 Kommentare

Daneisha Blair
Daneisha Blair am 24 Aug. 2021
I've received this error when I tried your approach.
>> TC2= arrayfun(@num2str,TC,'UniformOutput',false);
Error using num2str
Input to num2str must be numeric.
Daneisha Blair
Daneisha Blair am 24 Aug. 2021
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.
Daneisha Blair
Daneisha Blair am 24 Aug. 2021
I got it work by looping and using this arrayfun(@num2str,A,'UniformOutput',false).
Thank you!
Daneisha Blair
Daneisha Blair am 24 Aug. 2021
Wait, how can I get it in string array instead of cell array, please?
Daneisha Blair
Daneisha Blair am 24 Aug. 2021
I got it :) . string(arrayfun(@num2str,A,'UniformOutput',false)).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

darova
darova am 24 Aug. 2021

0 Stimmen

Try num2str
a = {nan 1 nan};
cellfun(@num2str,a,'UniformOutput',0)
ans = 1×2 cell array
{'NaN'} {'NaN'}

1 Kommentar

Daneisha Blair
Daneisha Blair am 24 Aug. 2021
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by