Find string within cell array

355 visualizaciones (últimos 30 días)
Sasquatch
Sasquatch el 6 de Oct. de 2014
Comentada: Davindra Usov el 21 de Jun. de 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?

Respuesta aceptada

Guillaume
Guillaume el 6 de Oct. de 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 comentarios
Ian Esten
Ian Esten el 8 de Mzo. de 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 el 21 de Jun. de 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?

Iniciar sesión para comentar.

Más respuestas (2)

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

Iniciar sesión para comentar.


Adam
Adam el 6 de Oct. de 2014
Editada: Adam el 6 de Oct. de 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 comentario
Guillaume
Guillaume el 6 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by