Find cell containing part of a string
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I would like to find the elements of a cell array that contain part of a specified string.
colorList = {'Red', 'Green', 'Blue', 'Purple'}; % List of values to match with
stringToCheck = 'Blue 23948723'; % String we are trying to match
I would like to return index=3 of colorList since that entry contains the stringToCheck text of 'Blue'. How can I do this?
0 comentarios
Respuesta aceptada
Más respuestas (2)
per isakson
el 26 de Oct. de 2017
Editada: per isakson
el 26 de Oct. de 2017
>> find( ismember( colorList, strsplit( stringToCheck ) ) )
ans =
3
or
>> find( ismember( colorList, strsplit( stringToCheck, {'\s','\.',','} ...
, 'CollapseDelimiters',true, 'DelimiterType','RegularExpression' ) ) )
ans =
3
if the color name is followed by a period or comma, e.g. "Blue.". And what about upper and lower case, e.g "blue"? And "Bluetooth"?
Akira Agata
el 26 de Oct. de 2017
If your MATLAB is R2016b or later version, you can use contains function, like:
idx = cellfun(@(x) contains(stringToCheck,x),colorList);
The answer is:
>> colorList(idx)
ans =
{'Blue'}
Ver también
Categorías
Más información sobre String en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!