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
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
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 个评论
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!