Trouble using strings in If statements using OR

8 views (last 30 days)
Sarah R
Sarah R on 20 Jan 2022
Commented: Stephen23 on 21 Jan 2022
I am trying to loop through and use IF statement as example:
if race == "H2" | "H1" | "H3" | "H4"
but I get error "Error using |
Input must be text or pattern."
If I try: race == "H2" || "H1" || "H3" || "H4"
I get error:
Conversion to logical from string is not possible.
"H1" and so forth are strings in Matlab
Thank you!

Accepted Answer

_ on 20 Jan 2022
if race == "H2" || race == "H1" || race == "H3" || race == "H4"
  1 Comment
Walter Roberson
Walter Roberson on 20 Jan 2022
Or alternately
if ismember(race, ["H1", "H2", "H3", "H4"])

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by