MATLAB Answers

Simulink - How to create a conditional "if" block with more than 3 conditions?

19 views (last 30 days)
Katarina Vuckovic
Katarina Vuckovic on 18 Sep 2019
Answered: David K. on 18 Sep 2019
Simulink - How do I create a conditional "if" block with more than 3 statements?
For example:
if(a>2)
do something
elseif (b>3)
do something else
elseif (c>4)
do something else
elseif (a<2 and c<2)
do something else
else
do default
end
What have I tried so far:
  • The default "if" block permits only 2 conditions + default.
  • I tried using the "case" but case only take one input and I need multiple inputs and 1 output (different depending on the conditions).

  4 Comments

Show 1 older comment
Katarina Vuckovic
Katarina Vuckovic on 18 Sep 2019
The "and" is not an issue. I used sudo code in the example above. The issue is that simulink is not allowing more than 2 conditional statements in an if/else if structure.
Nommah
Nommah on 18 Sep 2019
I'm running the script and it seems to be running fine for me.
a = 1;
b = 1;
c = 1;
if(a>2)
disp('first');
elseif (b>3)
disp('second');
elseif (c>4)
disp('third');
elseif (a<2 && c<2)
disp('fourth');
else
disp('default');
end
The issue I think you're facing is once a statement is true (e.g. if b is greater than 3, hence second if statement is correct). The program will exit the if statement and not check if a<2 && c<2.
One way to solve this issue (may not be the best but it should work) is just dividing the if statements into seperate statements instead of creating multiple elseifs.
Katarina Vuckovic
Katarina Vuckovic on 18 Sep 2019
I used the user defined function and it worked. Thank you David K.
Also, the image is very helpful and now I see how I can do it in the window.

Sign in to comment.

Accepted Answer

David K.
David K. on 18 Sep 2019
**Moved from questions to answer**
When you know the matlab code needed to do something in Simulink a Matlab Fuction Block can be added to solve the problem.
However, the IF block can also do multiple else ifs as shon below.
simulinkIFS.png

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.