7 381 views (last 30 days)

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

Jan
on 25 Feb 2011

Edited: MathWorks Support Team
on 8 Nov 2018

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'));

In previous versions of MATLAB, 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.

Duc Minh Nguyen
on 12 Jun 2018

This answer will cause problem if there are other strings containing 'bla' in the array. For exact searching, use strcmp instead:

idx = find(strcmp(C, 'bla'))

Jan
on 12 Jun 2018

Sign in to comment.

Jay
on 28 May 2016

Edited: Jay
on 28 May 2016

This worked for me:

idx = find(ismember(C, 'bla'))

ANKUR WADHWA
on 17 Jan 2020

Doesn't work on this one

C = { {'a'}, {'b'}, {'c'}, {'a'}, {'a'};{'b'}, {'a'}, {'c'}, {'a'}, {'c'} }

idx = find(strcmp([C{:}], 'a'))

Returns

idx = 1 4 7 8 9

Any suggestion on how to solve it.

Sign in to comment.

Matt B
on 14 Nov 2013

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:

cell_array={1,eye(2),true,'foo',10};

string='foo'

logical_cells = cellfun(cellfind('foo'),cell_array)

logical_cells =

[0,0,0,1,0]

Jos (10584)
on 25 Feb 2011

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

Omer Moussaffi
on 26 Feb 2017

Faster options: count startsWith endsWith

E,g, Index = count(Mycellarray, 'Bla');

Jan
on 25 Feb 2011

You can check if your cell is a cell string:

iscellstr(Mycellarray);

This displays the indices and contents of the non-strings:

Index = find(~cellfun('isclass', Mycellarray, 'char'))

disp(Mycellarray(Index));

Another idea is, that some strings are multi-row CHAR matrices:

Index = find(cellfun('size', Mycellarray, 1) > 1)

Sign in to comment.

Peter Farkas
on 9 May 2016

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.

Sign in to comment.

Mukesh Jadhav
on 9 Oct 2016

Edited: per isakson
on 10 Jan 2017

Haven't tested but this should work fine.

word_to_find=strfind(strarray,'stringtofind');

starray.index(word_to_find);

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_3926

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_3926

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_3934

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_3934

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_4088

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_4088

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_131581

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_131581

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_131604

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_131604

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_487774

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_487774

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_487780

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_487780

Sign in to comment.