MATLAB Answers

Find index of cells containing my string

5,043 ビュー (過去 30 日間)
New
New 2011 年 2 月 25 日
コメント済み: per isakson 2020 年 5 月 22 日
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
Jan 2013 年 2 月 22 日
@Felipe: Exactly. A cell is a cell string, if it contains strings only. And STRFIND works on strings and cell strings only.
Yusuf Arslan
Yusuf Arslan 2017 年 9 月 26 日
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
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
Jan 2011 年 2 月 25 日
編集済み: MathWorks Support Team 2018 年 11 月 8 日
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.

  8 件のコメント

表示 5 件の古いコメント
Jan
Jan 2018 年 6 月 12 日
@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'.
ANA ROSHAN
ANA ROSHAN 2020 年 2 月 10 日
it was very useful. thanks!
Nnamdi Njoku
Nnamdi Njoku 2020 年 5 月 22 日
Just what i needed, thanks

サインインしてコメントする。

その他の回答 (7 件)

Jay
Jay 2016 年 5 月 28 日
編集済み: Jay 2016 年 5 月 28 日
This worked for me:
idx = find(ismember(C, 'bla'))

  7 件のコメント

表示 4 件の古いコメント
Einat
Einat 2019 年 6 月 5 日
Yes! Worked just like I needed it to.
ANKUR WADHWA
ANKUR WADHWA 2020 年 1 月 17 日
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.
per isakson
per isakson 2020 年 5 月 22 日
It works as documented.
Matlab uses column-major order. (I fail to find a page to link to in the documentation.)
Try
%%
C = { {'a'}, {'b'}, {'c'}, {'a'}, {'a'}
{'b'}, {'a'}, {'c'}, {'a'}, {'c'} };
%%
cac = [C{:}]
idx = find(strcmp( cac, 'a' ))
%%
C1 = permute( C, [2,1] ); % switch rows and columns
idx = find(strcmp( [C1{:}], 'a' ))
it outputs
cac =
1×10 cell array
Columns 1 through 8
{'a'} {'b'} {'b'} {'a'} {'c'} {'c'} {'a'} {'a'}
Columns 9 through 10
{'a'} {'c'}
idx =
1 4 7 8 9
idx =
1 4 5 7 9

サインインしてコメントする。


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:
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
Sepp 2016 年 2 月 27 日
Great answer. Works very well.
josh gore
josh gore 2017 年 1 月 26 日
The inline function was a life saver!
Jan
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
AATIQ OUSSAMA 2017 年 4 月 16 日
Thanks Jos , It works fine !

サインインしてコメントする。


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.

サインインしてコメントする。


Jan
Jan 2011 年 2 月 25 日
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 件のコメント

サインインしてコメントする。


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.

  0 件のコメント

サインインしてコメントする。


Mukesh Jadhav
Mukesh Jadhav 2016 年 10 月 9 日
編集済み: per isakson 2017 年 1 月 10 日
Haven't tested but this should work fine.
word_to_find=strfind(strarray,'stringtofind');
starray.index(word_to_find);

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by