Greater than & lesser than works reversed in an if-statement that breaks

1 view (last 30 days)
Hello out there!
I have quite a problem figuring how to solve a problem that exists in a main-script of mine in Matlab-programming. I've made three functions that my main-script is based upon. I've startede with a while true statement. You can see the code below this:
Best regards
Kristian
  1 Comment
James Tursa
James Tursa on 5 May 2015
@Kristian: It is an abuse of this site and a disservice to the readers and contributors for you to edit out the content of your question after you have received answers.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 4 May 2015
Edited: Stephen23 on 4 May 2015
Your arguments to && are reversed. Read the documentation for the && operator and you will find that it evaluates from left to right. So first it evaluates this:
0 < beamLength
which if beamLength happens to be NaN (such as if the input is not a valid number) then the comparison is false and the next term is never evaluated: the results will always be false:
>> 0 < NaN
ans =
0
To resolve this you could try reversing the order:
~isnan(beamLength) && 0 < beamLength
But a better solution would be to not use break at all, and make the while-loop condition do the comparison for you:
val = NaN;
while ~(isfinite(val) && val>0)
val = str2double(input('enter a value: ','s'));
end
this will keep prompting until a positive number has been entered.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!