Within a cell array, how can I look for cells that contain a certain element, in addition to other elements I am not looking for?

1 回表示 (過去 30 日間)
For example, let's say I have a cell array:
Z={'abcde','atg';'va','rem';5,'a'}.
I want to find the positions (x,y) of all the cells that contain the letter 'a', among other letters that are in the same cell as well (I don't want to split the cell into 1 letter cells and then use strcmp).
Thanks

採用された回答

Guillaume
Guillaume 2015 年 2 月 5 日
Do you really have a numeric value in one of the cell of your cell array?
Anyway, strfind is the function you want
%only deal with strings
zwithonlystrings = Z(cellfun(@ischar, Z));
aposition = strfind(zwithonlystrings, 'a');
%if you want the result, the same shape as z:
aposinz = cell(size(Z));
aposinz(cellfun(@ischar, Z)) = aposition
  3 件のコメント
Iddo Weiner
Iddo Weiner 2015 年 2 月 5 日
OK just figured it out - the number is the position of 'a' within the cell.. Thanks for the help!
Guillaume
Guillaume 2015 年 2 月 5 日
Yes, and if you have several a in the strings, you'll get an array of all their position in the cell.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by