Simulink Reference    
If

Implement a C-like if-else control flow statement in Simulink

Library

Ports & Subsystems

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:

  1. Place an If block in the current system.
  2. Open the Block Parameters dialog of the If block and enter as follows:
  3. Create If Action subsystems to connect to each of the if, else, and elseif ports.
  1. These consist of a subsystem with an Action Port block. When you place an Action Port block inside each subsystem, an input port named Action is added to the subsystem.

  1. Connect each if, else, and elseif port of the If block to the Action port of an If Action subsystem.
  1. When you make the connection, the icon for the If Action block is renamed to the type of the condition that it attaches to.

  1. In each If Action subsystem, enter the Simulink blocks appropriate to the body to be executed for the condition it handles.
  1. In the preceding example, this is shown as body_1, body_2, and body_3.

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

Number of inputs
The number of inputs to the If block. These appear as data input ports labeled with a 'u' character followed by a number, 1,2,...,n, where n equals the number of inputs that you specify.
If expression
The condition for the 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 expressions
A string list of 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.
Show else condition
If this box is selected, an 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.
Enable zero crossing detection
Select to enable zero crossing detection. For more information, see Zero Crossing Detection in the Using Simulink documentation.

Characteristics

Direct Feedthrough
Yes
Sample Time
Inherited from driving block
Scalar Expansion
No
Dimensionalized
Yes


  IC If Action Subsystem