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일

0 개 추천

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일
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 2021년 8월 24일
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 2021년 8월 24일
I got it work by looping and using this arrayfun(@num2str,A,'UniformOutput',false).
Thank you!
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일

0 개 추천

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2021년 8월 24일

댓글:

2021년 8월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by