フィルターのクリア

Using strcmp with multiple inputs

51 ビュー (過去 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 件のコメント
John Doe
John Doe 2020 年 1 月 20 日
Thank you!

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by