MATLAB Answers

7

Find index of cells containing my string

New 님이 질문을 제출함. 25 Feb 2011
최근 활동 Jan 님이 댓글을 추가함. 6 Jun 2019
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

표시 이전 댓글 수: 4
Jan
on 22 Feb 2013
@Felipe: Exactly. A cell is a cell string, if it contains strings only. And STRFIND works on strings and cell strings only.
Hello, I have got a similar Problem.
- find(strcmp(rawdata,'ggiBoundaries1(1,1)'))
I want to find in rawdata the first entry of ggiBoundaries1. But it doesn't work. The error message is =
"0×1 empty double column vector".
When I enter "ggiBoundaries1(1,1)" the output is a char. Maybe that's the problem or not?
Jan
on 26 Sep 2017
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.

로그인 to comment.

답변 수: 8

Answer by Jan
on 25 Feb 2011
Edited by MathWorks Support Team on 8 Nov 2018
 Accepted Answer

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.

  댓글 수: 6

표시 이전 댓글 수: 3
Just to add my experience of running this. If the cell C contains some entry will NaN, it will gave the error like "First argument must be a string array, character vector, or cell array of character vectors" make sure your cell doesn't contain NaN
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
@Duc Minh Nguyen: As said in the answer, it is the purpose of the code to find any occurrences of 'bla', not just the string 'bla'.

로그인 to comment.


Answer by Jay
on 28 May 2016
Edited by Jay
on 28 May 2016

This worked for me:
idx = find(ismember(C, 'bla'))

  댓글 수: 5

표시 이전 댓글 수: 2
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'))
Great, thanks! Old stuff still makes it !
Yes! Worked just like I needed it to.

로그인 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]

  댓글 수: 3

Sepp
on 27 Feb 2016
Great answer. Works very well.
The inline function was a life saver!
Jan
on 6 Jun 2019
@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]

로그인 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

  댓글 수: 1

Thanks Jos , It works fine !

로그인 to comment.


Answer by Omer Moussaffi on 26 Feb 2017

Faster options: count startsWith endsWith
E,g, Index = count(Mycellarray, 'Bla');

  댓글 수: 1

Yes, this method should work well starting from R2016b.

로그인 to comment.


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

  댓글 수: 0

로그인 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.

  댓글 수: 0

로그인 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);

  댓글 수: 0

로그인 to comment.



Translated by