Does this character vector match any of the following character vectors?

2 ビュー (過去 30 日間)
Sarah
Sarah 2020 年 1 月 17 日
コメント済み: Sarah 2020 年 1 月 17 日
How can I check to see if a particular character vector or string matches anything in a list of other character vectors/strings? For example, if the current month is June, July, or August, I want to call the current season Summer; if not, I want to call the current season notSummer. I tried this:
if strcmp(currentMonth,'June'||'July'||'August')
currentSeason = 'Summer'
else
currentSeason = 'NotSummer'
end
But it gives me the error message "Operands to the || and && operators must be convertible to logical scalar values."
I also tried it with just commas instead of ||, but it said I had too many input arguments for strcmp().
How can I compare strings in this way?
Thank you.

採用された回答

James Tursa
James Tursa 2020 年 1 月 17 日
編集済み: James Tursa 2020 年 1 月 17 日
You could use cell arrays. E.g., use
ismember(currentMonth,{'June','July','August'})
instead of
strcmp(currentMonth,'June'||'July'||'August')
Or if you wanted a case insensitive check
any(strcmpi(currentMonth,{'June','July','August'}))

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by