If statement in loop with AND OR operands

1 回表示 (過去 30 日間)
DavidL88
DavidL88 2021 年 11 月 10 日
コメント済み: DavidL88 2021 年 11 月 10 日
I need an IF statement in a loop. I need a part of a script to skip where;
(Outcome is 11_right OR Outcome is 21_right) AND (ERP = earlyP3)
Tried both lines below but get "Operands to the || and && operators must be convertible to logical scalar values."
if ((Outcome{j} == '11_right') || (Outcome{j} == '21_right')) && (ERP{l} == 'earlyP3')
Also tried
if ismember(Outcome{j}, ['11_right', '21_right']) && ismember(ERP{l}, ['earlyP3'])

採用された回答

James Tursa
James Tursa 2021 年 11 月 10 日
編集済み: James Tursa 2021 年 11 月 10 日
The problem is that the comparison Outcome{j} == '11_right' is an array comparison, not a scalar comparison. I.e., the string is an array of characters, so the == operation compares each element and gives an array result. What you want to use is a string comparison function such as strcmp. E.g.,
strcmp(Outcome{j},'11_right')
And there are related functions such as strcmpi that ignore upper vs lower case in the comparison, etc.
  1 件のコメント
DavidL88
DavidL88 2021 年 11 月 10 日
Thanks James.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by