Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

If - Model if-else control flow

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.

if (u1 > 0) {
	body_1;
}
else if (u2 > 0){
	body_2;
}
else {
	body_3;
}

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:

    • Enter the Number of inputs field with the required number of inputs necessary to define conditions for the 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.

    • Enter the expression for the if condition of the 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.

    • Enter expressions for any elseif conditions of the 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.

    • Check the Show else condition check box to create an else output port.

      The else port is optional and not required for the operation of the If block.

  3. Create If Action subsystems to connect to each of the if, else, and elseif ports.

    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.

  4. Connect each if, else, and elseif port of the If block to the Action port of an If Action subsystem.

    When you make the connection, the icon for the If Action block is renamed to the type of the condition that it attaches to.

      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.

  5. In each If Action subsystem, enter the Simulink blocks appropriate to the body to be executed for the condition it handles.

      Note   All blocks in an If Action Subsystem must run at the same rate as the driving If block. You can achieve this by setting each block's sample time parameter to be either inherited (-1) or the same value as the If block's sample time.

    In the preceding example, the If Action subsystems are named body_1, body_2, and body_3.

Data Type Support

Inputs u1,u2,...,un can be scalars or vectors of any built-in Simulink data type and must all be of the same data type. The inputs cannot be of any user-defined type, such as an enumerated type. Outputs from the if, else, and elseif ports are Action signals to If Action subsystems that you create by using Action Port blocks and subsystems.

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. The expression must not contain data type expressions, e.g., int8(6), and must not reference workspace variables whose data type is other than double or single.

    Note   You cannot tune the If expression during accelerated-mode simulation (see Accelerating Models), in referenced models executing in Accelerator mode, or in code generated from the model. The If block also does not support custom storage classes.

Elseif expressions

A string list of elseif conditions delimited by commas. These conditions appear below the if port and above the else port if you select the Show else condition check box. 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. The expression must not contain data type expressions, e.g., int8(6), and must not reference workspace variables whose data type is other than double or single.

    Note   You cannot tune the Elseif expression during accelerated-mode simulation (see Accelerating Models), in referenced models executing in Accelerator mode, or in code generated from the model. The If block also does not support custom storage classes.

Show else condition

If you select this check box, 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.

Sample time

Specify the sample time of the input signal. See How to Specify the Sample Time in the online documentation for more information.

Examples

The If block does not directly support fixed-point data types. However, you can use the Compare To Constant block to work around this limitation.

For example, consider the following floating-point model.

In this model, the If Action subsystems use their default configurations. The block and simulation parameters for the model are set to their default values except as follows:

Block or Dialog Box

Parameter

Setting

Configuration Parameters — Solver pane

Start time

0.0

Stop time

1.0

Type

Fixed-step

Solver

Discrete (no continuous states)

Fixed-step size

0.1

Repeating Sequence Stair

Vector of output values

[-2 -1 1 2].'

Repeating Sequence Stair1

Vector of output values

[0 0 0 0 1 1 1 1].'

If

Number of inputs

2

If expression

(u1 > 0) | (u2 > 0.5)

Show else condition

selected

Constant

Constant value

-4

Constant1

Constant value

4

Scope

Number of axes

3

Time range

1

For this model, if input u1 is greater than 0 or input u2 is greater than 0.5, the output is 4. Otherwise, the output is -4. The Scope block shows the output, u1, and u2:

You can implement the same model with fixed-point data types:

The Repeating Sequence Stair blocks now output fixed-point data types.

The Compare To Constant blocks implement two parts of the If expression that is used in the If block in the floating-point version of the model, (u1 > 0) and (u2 > 0.5). The OR operation, (u1|u2), can still be implemented inside the If block. For a fixed-point model, the expression must be partially implemented outside of the If block as it is here.

The block and simulation parameters for the fixed-point model are the same as for the floating-point model with the following exceptions and additions:

Block

Parameter

Setting

Compare To Constant

Operator

>

 

Constant value

0

 

Output data type mode

Boolean

 

Enable zero-crossing detection

off

Compare To Constant1

Operator

>

 

Constant value

0.5

 

Output data type mode

Boolean

 

Enable zero-crossing detection

off

If

Number of inputs

2

 

If expression

u1|u2

Characteristics

Direct Feedthrough

Yes

Sample Time

Inherited from driving block

Scalar Expansion

No

Dimensionalized

Yes

Zero-Crossing Detection

Yes, if enabled

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS