Why does my code result in unlimited ans = 1, when that is not in my code?

1 次查看(过去 30 天)
I am trying to get the result of righton, lefton, and input, but when I run the code it creates unlimited ans = 1. When I stop the code with ctrl+c I end up getting the answer I want. Why does "Ans = 1" show up when there is no "Ans" in my code? Here is a picture of my code. my code. Thanks in advance for your help!
  2 个评论
per isakson
per isakson 2014-3-18
编辑:per isakson 2014-3-18
Your question is too hard for me to read, please markup!
The reason for ans = 1 is often that a function call without an ending ";" returns a logical true/false. ans is a special creature, which is created automatically.

请先登录,再进行评论。

采纳的回答

dpb
dpb 2014-3-18
编辑:dpb 2014-3-18
The else clause is improperly formed--it cannot have anything as a conditional whereas you've written
else (right & left & center) < thresh
What you're seeing is as if the code were
else
(right & left & center) < thresh
The ans is the result of that comparison.
I haven't read the logic; if you need that actual comparison then need another elseif for it; if it's intended as indicating what the meaning of the else actually is then comment it out like
else % (right & left & center) < thresh
  2 个评论
Jake Burger
Jake Burger 2014-3-18
I see now! Thank you, I tried your last option, as it turns out that statement is not needed since it is the only other option besides the two other elseif statements. Thanks again!
dpb
dpb 2014-3-18
I was little surprised Matlab didn't flag a syntax error--I'd've thunk it would have needed a endline character after the else. But, maybe that's just because I've written one-line if (test),action;end clauses for simple cases with the comma for so long out of habit I just never noticed that the comma might be omitted.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by