Find string within cell array

조회 수: 355 (최근 30일)
Sasquatch
Sasquatch 2014년 10월 6일
댓글: Davindra Usov 2022년 6월 21일
This is probably a dumb question but I can't seem to find anything on google that will allow me to do what I want. All I want to do is check a string against a cell array of strings. Let's say I have something like:
string = 'This is a string';
elements = {'string', 'cell'};
strfind(elements, string);
This returns nothing for me, and it makes me put the cell array first. This is checking for string in elements and I want to check for elements in string. If I swap the parameters like:
strfind(string, elements{1});
This works, but I want to check the entire array, and I think a loop in the code will look bad. Is there any way that I can check to see if any of the stings in my cell array are inside of the sentence?

채택된 답변

Guillaume
Guillaume 2014년 10월 6일
There's no built-in function for this. If all you want to know is whether each string in the cell array is present in your big string:
ispresent = cellfun(@(s) ~isempty(strfind(string, s)), elements)
  댓글 수: 3
Ian Esten
Ian Esten 2016년 3월 8일
Nice solution. I would suggest using strmatch instead of strfind. If any of your cells contain column vectors, strfind will complain, but strmatch will do what you want.
Davindra Usov
Davindra Usov 2022년 6월 21일
Do you happen to know how I can create another (nx1) cell array where each cell contains all of the column indices where the string is in?

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

추가 답변 (2개)

Chad Greene
Chad Greene 2014년 10월 6일
regexp is quite good at this.
  댓글 수: 3
Guillaume
Guillaume 2015년 1월 14일
Not sure why you're bringing this up on an old topic.
I love regular expressions, they're extremely powerful for parsing strings. They have their place however and if your search string is just a generic plain string as opposed to a regular expression, strfind is better. It should be faster and you don't have to worry about escaping the search string.
In any case, for your particular problem, neither regexp nor strfind are the right tool. You want ismember:
index = find(ismember(C, 'B'));
Kristoffer Walker
Kristoffer Walker 2019년 11월 10일
Love Guillaume's solution. Elegant, fast.

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


Adam
Adam 2014년 10월 6일
편집: Adam 2014년 10월 6일
I wanted to do this recently and was surprised none of the built-in string-based functions worked for this so I delved into regexp for the first time...
regexp( string, elements );
will give you the indices into your string to the start of any matching elements. By default the result would be a cell array of length equal to the length of your elements cell array and in each cell of the result will be an array of indices to the start of any matches of that element.
e.g.
string = 'This is a string not a cell, yes really a string, not a cell'; elements = {'string', 'cell'};
res = regexp( string, elements )
res =
[1x2 double] [1x2 double]
>> res{1}
ans =
11 43
>> res{2}
ans =
24 57
If you want you can get other outputs from regexp instead which are detailed in the help at
doc regexp
if you follow the 'outkey' link to find the options.
  댓글 수: 1
Guillaume
Guillaume 2014년 10월 6일
This is dangerous if any of the search string contain a special regular expression character (such as ., +, $, etc) as it will then be interpreted as something else. To be safe, elements should be first be escaped with
regexptranslate('escape', elements)
Still, considering the search strings are not regular expression, a whole regular expression engine is a bit overkill.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by