This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

If

Select subsystem execution using logic similar to if-else statement

  • Library:
  • Simulink / Ports & Subsystems

Description

The If block, along with If Action Subsystem blocks containing an Action Port block, implements if-else logic to control subsystem execution. For an example using the If block, see If Action Subsystems.

Limitations

The If block has the following limitations:

  • It does not support tunable parameters. Values for an if or elseif expression cannot be tuned during a simulation in normal or accelerator mode, or when running generated code.

    To implement tunable if-else expressions, tune the expression outside the If block. For example, use the Relational Operator block to evaluate the expression outside of the If block or add the tunable parameter as an input to the If block.

  • It does not support custom storage classes. See Apply Custom Storage Classes to Individual Signal, State, and Parameter Data Elements (Embedded Coder).

  • The If expression and Elseif expressions cannot accept certain operators, such as +, -, *, and /.

Ports

Input

expand all

Inputs u1,u2,...,un must have the same data type. The inputs cannot be of any user-defined type, such as an enumerated type.

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. See Support for Fixed-Point Data Type in If Action Subsystems.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32

Output

expand all

Outputs from the if, else, and elseif ports are action signals to If Action Subsystem blocks.

Support for Fixed-Point Data Type

Parameters

expand all

1

Specify one input port.

integer

Specify the number of input ports. Block ports are labeled with a 'u' character followed by a number, 1,2,...,n, where n equals the number of inputs that you specify.

Programmatic Use

Block Parameter: NumInputs
Type: character vector
Values: '1' | '<integer>'
Default: '1'

The If Action Subsystem attached to the if port executes when its associated expression evaluates to true.

u1 > 0

Specify sending an action signal on the output port when the input u1 is greater than 0.

logical expression

Specify logical expression. This expression appears on the If block adjacent to the if output port.

The expression can include only the operators <, <=, ==, ~=, >, >=, &, |, ~, (), unary-minus. Operators such as +, -, *, /, and ^ are not allowed. The expression must not contain data type expressions, for example, int8(6), and must not reference workspace variables whose data type is other than double or single.

Programmatic Use

Block Parameter: IfExpression
Type: character vector
Values: 'u1 > 0' | '<logical expression>'
Default: 'u1 > 0'

The If Action Subsystem attached to an elseif port executes when its expression evaluates to true and all if and elseif expressions are false.

empty

Logical expressions not specified.

list of logical expressions

Specify a list of logical expressions delimited by commas. The expressions appear on the If block below the if port and above the else port when you select the Show else condition check box.

Expressions can include only the operators <, <=, ==, ~=, >, >=, &, |, ~, (), unary-minus. Operators such as +, -, *, /, and ^ are not allowed. The expressions must not contain data type expressions, for example, int8(6), and must not reference workspace variables whose data type is other than double or single.

Programmatic Use

Block Parameter: ElseIfExpressions
Type: character vector
Values: '' | '<list of logical expressions>'
Default: ''

When the if port and all elseif port expressions are false, the else port sends an action signal to execute the attached If Action Subsystem block.

on

Display else port.

off

Hide else port.

Programmatic Use

Block Parameter: ShowElse
Type: character vector
Values: 'on' | 'off'
Default: 'on'

Control zero-crossing detection.

on

Detect zero crossings.

off

Do not detect zero crossings.

Programmatic Use

Block Parameter: ZeroCross
Type: character vector
Values: 'on' | 'off'
Default: 'on'

Block Characteristics

Data Types

double | single | Boolean | base integer

Direct Feedthrough

No

Multidimensional Signals

No

Variable-Size Signals

No

Zero-Crossing Detection

Yes

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Introduced before R2006a