Find index of cells containing my string

조회 수: 5,832(최근 30일)
New 2011년 2월 25일
댓글: Cris LaPierre 2022년 1월 13일 19:12
Hi, I have a cell aray (40,000X1)in which every cell contains a string. I would like to find the indexes of the cells containing a specific string.
I used the following:
Index = strfind(Mycellarray, 'Bla');
I get the following error: ??? Error using ==> cell.strfind at 35 If any of the input arguments are cell arrays, the first must be a cell array of strings and the second must be a character array.
What is wrong, in the help file strfind accepts cell arrays and a pattern...?
Thank you
  댓글 수: 7
Jan 2017년 9월 26일
This is not an error message. It simply tells you, that the string is not found.
Are you really looking for the string 'ggiBoundaries1(1,1)' ? Or do you mean:
find(strcmp(rawdata, ggiBoundaries1(1,1)))
to search for occurrences of the first character of the variable ggiBoundaries1?
Please do not append a new question to an existing thread. Better open a new one. Thanks.

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

채택된 답변

Jan 2011년 2월 25일
편집: MathWorks Support Team 2021년 3월 23일
Do you want to search for 'bla' within the text in each element of the cell array, or for elements that are 'bla' exactly? If you explain this detail, then your question would be easier to answer.
If you are searching for text that has 'bla' as part of the text, then starting in R2016b you can use the “contains” function, as Alexander Cranney pointed out.
Index = find(contains(C,'bla'));
The function "contains" returns a logical array. This type of logical indexing can be used to make many workflows more efficient. For more on using logical arrays, refer to the documentation:
In previous versions of MATLAB (before R2016b), you can use the “strfind” function. However, “strfind” returns a cell array of indices. For any input cell whose text does not contain 'bla', “strfind” returns an empty cell. Use “isempty” and “cellfun” with the “find” function to find the empty cells.
IndexC = strfind(C,'bla');
Index = find(not(cellfun('isempty',IndexC)))
If you are searching for text that is exactly 'bla', then see Jos’ answer.
  댓글 수: 10
Walter Roberson
Walter Roberson 2021년 1월 22일
exact_match_mask = strcmp(YourCell, 'bla')
exact_match_locations = find(exact_match_mask)
provided that your cell entries are all character vectors.

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

추가 답변(7개)

Jay 2016년 5월 28일
편집: Jay 2016년 5월 28일
This worked for me:
idx = find(ismember(C, 'bla'))
  댓글 수: 8

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

Matt B
Matt B 2013년 11월 14일
I realize this question is old now, but a simple way of doing this is to define an inline function:
cellfind = @(string)(@(cell_contents)(strcmp(string,cell_contents)));
You can then use this with cellfun to return a boolean value for each element of the cell. For example:
logical_cells = cellfun(cellfind('foo'),cell_array)
logical_cells =
  댓글 수: 3
Jan 2019년 6월 6일
@Matt B: strcmp accepts a cell array directly, so you can avoid the complicated cellfun approach with the expensive anonymous function:
cell_array = {1,eye(2),true,'foo',10}
strcmp(cell_array, 'foo')
>> [0,0,0,1,0]

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

Jos (10584)
Jos (10584) 2011년 2월 25일
So, your original array was a cell array of cells with a single string. Use STRCMP and FIND to get the indices of the cells with a cell containing the specified string
C = { {'a'}, {'b'}, {'c'}, {'a'}, {'a'} } % data
idx = find(strcmp([C{:}], 'a')) % single line engine
  댓글 수: 1
AATIQ OUSSAMA 2017년 4월 16일
Thanks Jos , It works fine !

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

Jan 2011년 2월 25일
You can check if your cell is a cell string:
This displays the indices and contents of the non-strings:
Index = find(~cellfun('isclass', Mycellarray, 'char'))
Another idea is, that some strings are multi-row CHAR matrices:
Index = find(cellfun('size', Mycellarray, 1) > 1)

Omer Moussaffi
Omer Moussaffi 2017년 2월 26일
Faster options: count startsWith endsWith
E,g, Index = count(Mycellarray, 'Bla');
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 2월 26일
Yes, this method should work well starting from R2016b.

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

Peter Farkas
Peter Farkas 2016년 5월 9일
You can also explicitelly define the index matrix:
[rw, ~] = size(cellArray);
ind = [1:1:rw];
idx = strcmp(cellArray, stringToSearchFor);
yourResult = ind(idx);
It is kind of verbose, if you review the code in 2 years time, you will still know what is going on.

Mukesh Jadhav
Mukesh Jadhav 2016년 10월 9일
편집: per isakson 2017년 1월 10일
Haven't tested but this should work fine.

Community Treasure Hunt

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

Start Hunting!

Translated by