using strcmp on nested cell array (cell array within a cell array)

22 ビュー (過去 30 日間)
ChiPhi85
ChiPhi85 2019 年 1 月 24 日
コメント済み: ChiPhi85 2019 年 1 月 25 日
I need to retrieve the index of a string within a cell array where some cells are strings and others are cell arrays of different sizes. For example the first 5 rows might look like this:
{1×2 cell}
{'Text1' }
{1×2 cell}
{'Text2' }
{1×5 cell}
How do I retrieve the row index when the string might be contained with the cell arrays? I'd like to do this without a loop and I'm sure there's a way but I can't figure it out and can't seem to find an answer anywhere. I've tried cellfun which works if I was searching for 'Text1' for example.

採用された回答

Guillaume
Guillaume 2019 年 1 月 24 日
I'm not entirely clear if you want to look inside the inner cell arrays or not.
If not:
lookup = 'something';
rows = find(cellfun(@(c) ischar(c) && strcmp(c, lookup), yourcellarray)) %the && is essential to ensure that strcmp is only called when the content of the cell is a char vector
If you want to look insie the inner cell arrays and only care about knowing which rows of the outer cell array may contain your search:
lookup = 'something';
rows = find(cellfun(@(c) any(strcmp(c, lookup)), yourcellarray))
Note that this assumes that the inner cells all contain char vectors.
  1 件のコメント
ChiPhi85
ChiPhi85 2019 年 1 月 25 日
I did want to look inside the cell arrays and I was missing the any function - thanks

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

その他の回答 (1 件)

Kevin Phung
Kevin Phung 2019 年 1 月 24 日
編集済み: Kevin Phung 2019 年 1 月 24 日
if a is your cell array:
myText ='Text1';
idx = find(cellfun(@ischar,a)) %returns those indices where cell is a chr array
idx = idx(find(strcmp(a(cellfun(@ischar,a)),myText)))

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by