Simulink - How to create a conditional "if" block with more than 3 conditions?
73 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Katarina Vuckovic
el 18 de Sept. de 2019
Comentada: Nachiket Bapat
el 23 de Feb. de 2021
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 comentarios
Nom
el 18 de Sept. de 2019
Editada: Nom
el 18 de Sept. de 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.
Respuesta aceptada
David K.
el 18 de Sept. de 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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238790/image.png)
1 comentario
Nachiket Bapat
el 23 de Feb. de 2021
can someone let me know what im doing wrong ?
Más respuestas (0)
Ver también
Categorías
Más información sobre Simulink Functions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!