フィルターのクリア

Skipping elseif when first condition is true

10 ビュー (過去 30 日間)
g
g 2019 年 1 月 21 日
回答済み: Steven Lord 2019 年 1 月 21 日
I currently have the following scheme:
if (a =0)
q = 4
elseif (b=0)
q = 5
rest of code
end
The problem is, in the case a =0 the program does not work properly, as it simply skips the "rest of code".
I have tried moving the "end" before rest of code, but that does not work for the purposes of this.
Is there a way such that in the case a =0, it sets q=4, then just doesn't consider the elseif (b=0) case and continues onto the rest of the code?
Thanks!
  1 件のコメント
Stephen23
Stephen23 2019 年 1 月 21 日
編集済み: Stephen23 2019 年 1 月 21 日
"The problem is, in the case a =0 the program does not work properly"
Read the MATLAB documentation to learn what operator to use for logical equivalence:
And for other logical operators:
Guessing is not a very efficient way to write code.

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

回答 (2 件)

Image Analyst
Image Analyst 2019 年 1 月 21 日
That's not how you check if a equals zero. You need to use double equals. A single equals is assignment, NOT comparison. Try this:
if (a == 0)
q = 4
elseif (b == 0)
q = 5
rest of code
end

Steven Lord
Steven Lord 2019 年 1 月 21 日
If you want to execute the rest of the code regardless of whether a was equal to 0 or b was equal to 0, take it out of the if / elseif / end block entirely.
if (a ==0)
q = 4
elseif (b==0)
q = 5
end
rest of code
If you need to throw an error if neither a nor b was 0, use else and throw an error inside the else.
if (a ==0)
q = 4
elseif (b==0)
q = 5
else
error('Expected either a or b to be 0')
end
rest of code

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by