Effacer les filtres
Effacer les filtres

Using strcmp with multiple inputs

51 vues (au cours des 30 derniers jours)
John Doe
John Doe le 20 Jan 2020
Modifié(e) : John Doe le 3 Avr 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!

Réponse acceptée

Stephen23
Stephen23 le 20 Jan 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
  1 commentaire
John Doe
John Doe le 20 Jan 2020
Thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

Allen
Allen le 20 Jan 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'}));

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by