While clause with multiple comparison strings

1 回表示 (過去 30 日間)
Andraz
Andraz 2024 年 2 月 9 日
コメント済み: Andraz 2024 年 2 月 16 日
I'm still a newbie in matlab coding therefore I need a bit help. It seems there is a problem in the following code. Whatever string I enter (including ScenarioA, ScenarioB, ScenarioC or ScenarioD) I always get the matching condition. For info - I have no problem with just one comparison string. Thanks in advance for any help.
Andrazko
%%%%%%%%%%%%%%%%
Matlab code:
SelectedScenario = input('Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"! \n',"s");
while SelectedScenario ~= "ScenarioA" || SelectedScenario ~= "ScenarioB" || SelectedScenario ~= "ScenarioC" || SelectedScenario ~= "ScenarioD"
fprintf("Your selected scenario isn't listed! ");
SelectedScenario = input('Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"! \n',"s");
end
%%%%%%%%%%%%%%%%
Command Window printout:
Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"!
ScenarioA
Your selected scenario isn't listed! Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"!
%%%%%%%%%%%%%%%%
  1 件のコメント
Stephen23
Stephen23 2024 年 2 月 9 日
編集済み: Stephen23 2024 年 2 月 9 日
Your logic is incorrect. The mistake you are making is corrected by understanding De Morgan's law:
In any case, do not chain together lots of individual logical comparisons: just use ISMEMBER or MATCHES on the whole list of possible options, with one single negation out in front. In other words, learn to think in terms of arrays (not lots of individual commands).

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

採用された回答

Shivam
Shivam 2024 年 2 月 9 日
編集済み: Shivam 2024 年 2 月 9 日
Hi Andraz,
Upon reviewing the code snippet you have shared, I notice there's a logical misstep causing the persistent loop condition regardless of the input string. Your loop intends to prompt the user until one of the specified scenarios is entered ('ScenarioA,' 'ScenarioB,' 'ScenarioC,' or 'ScenarioD').
The issue is because of the logical operator OR (||) operator used because the while condition will always evaluate to true because a string cannot be simultaneously different from all the other strings.
To resolve this, you should use the AND (&) logical operator. This operator will ensure that the loop only continues if the entered SelectedScenario does not correspond with any of the four provided scenarios.
You can refer to the following workaround:
SelectedScenario = input('Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"! \n',"s");
while SelectedScenario ~= "ScenarioA" && SelectedScenario ~= "ScenarioB" && SelectedScenario ~= "ScenarioC" && SelectedScenario ~= "ScenarioD"
fprintf("Your selected scenario isn't listed! ");
SelectedScenario = input('Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"! \n',"s");
end
I believe the explanation and modified code address the issue.
Thanks
  1 件のコメント
Andraz
Andraz 2024 年 2 月 16 日
Hi Shivam,
Now it works. Thanks a lot!
Andraz

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeManage Products についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by