Using strcmp with multiple inputs

조회 수: 48 (최근 30일)
John Doe
John Doe 2020년 1월 20일
편집: John Doe 2020년 4월 3일
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!

채택된 답변

Stephen23
Stephen23 2020년 1월 20일
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개)

Allen
Allen 2020년 1월 20일
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'}));

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by