Asked by New
on 25 Feb 2011

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

*No products are associated with this question.*

Answer by Jan Simon
on 25 Feb 2011

Edited by Jan Simon
on 7 Mar 2017

Accepted Answer

Do you want to search for the string 'bla' inside each string of the cell array, or should the cell element be 'bla' exactly? The answering would be easier, if you explain this detail.

If 'bla' should be part of the string:

IndexC = strfind(C, 'bla'); Index = find(not(cellfun('isempty', IndexC)));

If the string should be excatly 'bla': See Jos' answer.

**[EDITED]**
Alexander Cranney explained, that MATLAB 2016b has a command for this:

Index = find(contains(C,'bla'));

Walter Roberson
on 26 Feb 2011

Possibly bla might occur multiple times and the indices of all of the positions is what is desired?

Pooya89
on 3 Nov 2012

A little correction to Jan's answer:

IndexC = strfind(C, 'bla'); % not C2 ...

No need to convert C into string from cell : C2 = [C{:}];

Alexander Cranney
on 6 Mar 2017

As of MATLAB 2016b, there is a new function "contains" that does exactly this!

% code Index = find(contains(C,'bla'));

Log in to comment.

Answer by 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]

Answer by Jay
on 28 May 2016

Edited by Jay
on 28 May 2016

This worked for me:

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

Narayanan Rengaswamy
on 4 Oct 2016

Great! Works clean. Thanks.

Cameron Kimbrough
on 10 Oct 2016

Nice, just what I was looking for.

Omer Moussaffi
on 19 Apr 2017 at 8:24

And this is faster, too. On my lenovo T460s, a search of a 3000-long cellstring takes about 0.5ms, compared with 2ms with

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

Log in to comment.

Answer by 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

Answer by Jan Simon
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)

Log in to comment.

Answer by Omer Moussaffi
on 26 Feb 2017

Faster options: count startsWith endsWith

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

Walter Roberson
on 26 Feb 2017

Yes, this method should work well starting from R2016b.

Log in to comment.

Answer by 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.

Log in to comment.

Answer by Mukesh Jadhav
on 9 Oct 2016

Edited by 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);

Log in to comment.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 5 Comments

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_3926

What version are you using?

>> Mycellarray = {'hello';'what';'is';'Bla';'about'};

>> strfind(Mycellarray,'Bla')

ans =

[]

[]

[]

[1]

[]

I will wildly speculate that you have a cell array in which each element is a cell array that contains a string.

## Jos (10584) (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_3934

Are you really sure you have a cell array of strings? Check it with ISCELLSTR

If this is not true, then you need to convert it first to be able to use STRFIND. For instance, if Walter is right and you have a cell array of a cell array of strings,

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

iscellstr(C) % false !

% ... but in this particular case it is easy to convert:

C2 = [C{:}]

and perhaps STRCMP better suits your needs as it will return a logical array directly

strcmp(C2,'a')

## New (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_4088

Hi

Thank you for your answers. I used C2 = [C{:}] as advised but then still trying to get the list of Indices I used:

Index = strfind(C2,'bla')

I get a cell array in which every cell is either empty [] or 1 but no list of indices.

K.

## Felipe (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_131581

I found out that if the cell has empty indexes it wont work. Example:

a =

>> strfind(a,'hey')

??? 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.

>> a{3} = 'letsGo'

a =

>> strfind(a,'hey')

ans =

It is not considered as a cell string if all the cells in the array have a string in them.

## Jan Simon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/2015-find-index-of-cells-containing-my-string#comment_131604

@Felipe: Exactly. A cell is a cell string, if it contains strings only. And STRFIND works on strings and cell strings only.

Log in to comment.