Find string within cell array

355 次查看(过去 30 天)
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