Simulink Reference |
Implement a C-like if-else
control flow statement in Simulink
Library
Description
The If block, along with If Action subsystems containing Action Port blocks, implements standard C-like if-else
logic.
The following shows a completed if-else
control flow statement.
In this example, the inputs to the If block determine the values of conditions represented as output ports. Each output port is attached to an If Action subsystem. The conditions are evaluated top down starting with the if
condition. If a condition is true, its If Action subsystem is executed and the If block does not evaluate any remaining conditions.
The preceding if-else
control flow statement can be represented by the following pseudocode.
You construct a Simulink if-else
control flow statement like the preceding example as follows:
if-else
control flow statement.
Elements of vector inputs can be accessed for conditions using (row, column) arguments. For example, you can specify the fifth element of the vector u2 in the condition u2(5) > 0 in an If expression or Elseif expressions field.
if-else
control flow statement in the If expression field.
This creates an if output port for the If block with a label of the form if(condition)
. This is the only required If Action signal output for an If block.
if-else
control flow statement in the Elseif expressions field.
Use a comma to separate one condition from another. Entering these conditions creates an output port for the If block for each condition, with a label of the form elseif(condition)
. elseif ports are optional and not required for operation of the If block.
Note
During simulation of an if-else control flow statement, the Action signal lines from the If block to the If Action subsystems turn from solid to dashed.
|
Data Type Support
Inputs u1,u2,...,un
can be scalar or vector of any data type, including fixed-point data types, except int64
and uint64
.
Outputs from the if, else, and elseif ports are Action signals to If Action subsystems that are created with Action Port blocks and subsystems. See Action Port
.
Parameters and Dialog Box
'u'
character followed by a number, 1,2,...,n
, where n
equals the number of inputs that you specify.if
output port. This condition appears on the If block adjacent to the if
output port. The if
expression can use any of the following operators: <. <=, ==, ~=, >, >=, &, |, ~, (), unary-minus
. The If Action subsystem attached to the if
port executes if its condition is true.elseif
conditions delimited by commas. These conditions appear below the if
port and above the else
port if the Show else condition check box is selected. elseif
expressions can use any of the following operators: <. <=, ==, ~=, >, >=, &, |, ~, (), unary-minus
. The If Action subsystem attached to an elseif
port executes if its condition is true and all of the if
and elseif
conditions are false.else
port is created. The If Action subsystem attached to the else
port executes if the if
port and all the elseif
ports are false.Characteristics
Direct Feedthrough |
Yes |
Sample Time |
Inherited from driving block |
Scalar Expansion |
No |
Dimensionalized |
Yes |
IC | If Action Subsystem |