Using strcmp with multiple inputs

45 次查看(过去 30 天)
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