Using strcmp with multiple inputs

49 visualizaciones (últimos 30 días)
John Doe
John Doe el 20 de En. de 2020
Editada: John Doe el 3 de Abr. de 2020
Hello everyone,
I have a question, I want to use strcmp but for multiple inputs. For example if this row contain THIS or THAT.
This is what I'm using
B = find(strcmp(rw(:,3),' Dr limited' ));
what I want
B = find(strcmp(rw(:,3),'Dr limited' | 'Dr Limited' ));
because sometimes it can be a capital or the last name changes. So I want to know if I can put all the possibilities and get where to find them. Or even put the word 'Dr' the get the locations.
Thank You!

Respuesta aceptada

Stephen23
Stephen23 el 20 de En. de 2020
You could use strfind or a regular expression to help you, e.g.:
>> ixc = cellfun(@ischar,rw(:,3));
>> ixc(ixc) = ~cellfun('isempty',regexpi(rw(ixc,3),'Boskalis','once'));
>> idx = find(ixc)
idx =
1
58
108

Más respuestas (1)

Allen
Allen el 20 de En. de 2020
You can use strcmpi, which is a non-case-sensitive version of strcmp.
B = find(strcmpi(rw(:,3),'boskalis westminster dredging limited'));
Also, if you are trying to determine which rows of rw contain this string, the use of find may not be the most efficient method. Try considering the following.
% Since strcmp and strcmpi return a boolean array, you can use that result as an index and extract only
% the rows matching your specified string.
B = rw(strcmpi(rw(:,3),'boskalis westminster dredging limited'),:);
Additionally, if you are trying to match one of multiple strings to a given string you can do so by using a cell-array of specified strings.
B = any(strcmp(rw(:,3),{'Boskalis Westminster Dredging limited','Boskalis Westminster Dredging Limited'}));

Categorías

Más información sobre Characters and Strings 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!

Translated by