What is the difference between if-else block and a switch block in simulink?

조회 수: 54 (최근 30일)
chethan
chethan 2014년 9월 4일
댓글: Gabriel Sobola 2021년 12월 4일
Can anyone explain the difference between if-else block and switch block in simulink?

답변 (4개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 9월 4일
  1. The switch block has 2 input signals and a third input (in the middle of the block) that indicate if the first or second signal will pass through the block. It'd the only action you can do, choosing between signal1 or signal2
  2. With If-Else block, when the condition is true, you can run any action you want
  댓글 수: 3
Matthias Seibold
Matthias Seibold 2014년 9월 4일
switch block:
the switch block in simulink is analogous to an if-else statement in matlab. you can can just switch between 2(!!) signals. therefore you choose your criteria to switch between the signals.
if block:
the if block does mostly the same. the difference is that you can add more elseif expressions, so you can include more possibilities than 2.
chethan
chethan 2014년 9월 4일
Now can you explain the difference between multiport switch block and if else block?

댓글을 달려면 로그인하십시오.


Matthias Seibold
Matthias Seibold 2014년 9월 4일
편집: Matthias Seibold 2014년 9월 4일
switch block:
the switch block in simulink is analogous to an if-else statement in matlab. you can can just switch between 2(!!) signals. therefore you choose your criteria to switch between the signals.
if block:
the if block does mostly the same. the difference is that you can add more elseif expressions, so it can include more possibilities than 2.

Matthias Seibold
Matthias Seibold 2014년 9월 4일
the multiport switch represents the switch-case statement. You just define different cases and the block activates the port of the true statement.
switch var
case 3
a = 10;
case 2
a = 20;
end
the if-else block does the same once again. the different options are set by elseif conditions. it represents a statement like:
if var > 3
a = 2;
elseif 0 < var =< 3
a = 1;
else
a = 0;
end
as you can see, you can define ranges for the variables in the if-else statement (if-block) that you cant realize with an switch-case statement (multiport switch block)
Does this answer your question?
  댓글 수: 2
Gabriel Sobola
Gabriel Sobola 2021년 12월 4일
Kindly look for the if-else block or switch block in the Simulink Library, and implement the statement above.

댓글을 달려면 로그인하십시오.


Gabriel Sobola
Gabriel Sobola 2021년 12월 4일
The if-else and the switch blocks are similar to if-else and switch case statements in Matlab script.
Both perform the same function when the input conditions/signals are two (just like in script).
For instance, if you want to decide between two conditions, you can use either of the blocks in Simulink, but if the conditions are more than two, if-else block is the prefered option.

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by