Why does my code result in unlimited ans = 1, when that is not in my code?
1 view (last 30 days)
Show older comments
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 Comments
per isakson
on 18 Mar 2014
Edited: per isakson
on 18 Mar 2014
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.
Accepted Answer
dpb
on 18 Mar 2014
Edited: dpb
on 18 Mar 2014
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 Comments
dpb
on 18 Mar 2014
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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!