Find string within cell array

422 views (last 30 days)
Sasquatch
Sasquatch on 6 Oct 2014
Commented: Davindra Usov on 21 Jun 2022
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?

Accepted Answer

Guillaume
Guillaume on 6 Oct 2014
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 Comments
Ian Esten
Ian Esten on 8 Mar 2016
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 on 21 Jun 2022
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?

Sign in to comment.

More Answers (2)

Chad Greene
Chad Greene on 6 Oct 2014
regexp is quite good at this.
  3 Comments
Guillaume
Guillaume on 14 Jan 2015
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 on 10 Nov 2019
Love Guillaume's solution. Elegant, fast.

Sign in to comment.


Adam
Adam on 6 Oct 2014
Edited: Adam on 6 Oct 2014
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 Comment
Guillaume
Guillaume on 6 Oct 2014
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.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!