if statements not executing as expected

8 views (last 30 days)
I'm experiencing problems with if statements under certain conditions. I'm using code within a Simulink embedded Matlab function block that looks something like this:
function setting = setting_select(error)
if error == -0.1
setting = 3;
else
setting = 4;
end
This looks trivial but the setting = 3 statement never executed even if error does indeed equal -0.1. The error signal is computed in a separate embedded Matlab block. However, when I replace the error signal with a constant (-0.1) the statement executes correctly. There are no algebraic loops nor continuous states in the model. I've tried casting data types in case the conditional statement is comparing different number formats but that didn't work either.
Thanks for your help.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Apr 2011
Please see the FAQ on this topic.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!