Replacing NaN with some specific values in a mix data type of cell.

2 ビュー (過去 30 日間)
Pin-Hao Cheng
Pin-Hao Cheng 2017 年 10 月 18 日
コメント済み: Jan 2017 年 10 月 18 日
Hi there, I'm looking for functions that can replace Nan with some specific values in a mix data type of cell. Mix data type as in there's some cell containing strings, some are containing num.
I tried using
table(cellfun(@isnan,table))={'0'}
and I got this
Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
Please help. Thanks!

採用された回答

Jan
Jan 2017 年 10 月 18 日
編集済み: Jan 2017 年 10 月 18 日
Start with a simple loop:
for iC = 1:numel(C)
aC = C{iC};
if isfloat(aC) && ~isempty(aC) % Only SINGLE and DOUBLE can be NaN
aC(isnan(aC)= 0;
C{iC} = aC;
end
end
  2 件のコメント
Pin-Hao Cheng
Pin-Hao Cheng 2017 年 10 月 18 日
Hi, thanks for your fast response! But somehow it doesn't work. I edited a little and it works now! Thanks for guiding!
for iC = 1:numel(C)
aC = C{iC};
if isfloat(aC) && isempty(aC) % Only SINGLE and DOUBLE can be NaN
aC = 0
C{iC} = aC;
end
end
Jan
Jan 2017 年 10 月 18 日
This replaces all empty cells by 0 and NaN does not appear at all. If you want this, use the simpler and faster:
C(cellfun('isempty', C)) = {0};
If your problem is not solved now, please post a small example of the input and output. My code converts:
C = {1, [2, NaN], 'hello', [], NaN}
to
C = {1, [2, 0], 'hello', [], 0}
And your code does:
C = {1, [2, NaN], 'hello', 0, NaN}

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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