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?

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

 채택된 답변

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

Thank you, the code works well. Yeah, I really do have some numeric values in random cells throughout the array. Just wanted to ask, the output I receive is the following:
aposinz =
[1] [1]
[2] []
[] [1]
what is the meaning of the 2 (in cell 2,1)?
thanks again..
OK just figured it out - the number is the position of 'a' within the cell.. Thanks for the help!
Yes, and if you have several a in the strings, you'll get an array of all their position in the cell.

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2015년 2월 5일

댓글:

2015년 2월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by