How do I identify each element in a cell array as a string or a number?

I have a cell array A = {2,8,10,'Good',15,3,21,'Morning',12,26,9,'Joe'}. I want to perform a function on each element depending on whether it is a string or a number. So far, I have tried ischar but it returns 0 for all elements.

 採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 5 月 16 日
out = cellfun(@ischar,A)

その他の回答 (1 件)

KL
KL 2017 年 5 月 16 日
A = {2,8,10,'Good',15,3,21,'Morning',12,26,9,'Joe'};
q = cellfun(@(x) isnumeric(x) && numel(x)==1, A);
numA = A(q)
charA = A(~q)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2017 年 5 月 16 日

回答済み:

KL
2017 年 5 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by