If
Select subsystem execution using logic similar to if-else
statement
Libraries:
Simulink /
Ports & Subsystems
Description
The If block, along with If Action Subsystem blocks that contain an Action Port block, implements if-else logic to control subsystem execution. For an example that uses the If block, see Select Subsystem Execution.
Examples
Simulink Subsystem Semantics
This set of examples shows different types of Simulink® Subsystems and what semantics are used when simulating these subsystems. Each example provides a description of the model and the subtleties governing how the model is executed.
If-Then-Else Blocks
The effect of feeding a sine wave into If Action Subsystem blocks. It is designed to illustrate the similarity between the If Action Subsystem block and the Enabled Subsystem block.
Modeling Clutch Lock-Up Using If Blocks
Use If/Else subsystems to build a clutch model. An 'If' subsystem models the clutch dynamics in the locked position while an 'Else' subsystem models the unlocked position. One or the other is enabled using the 'If' block. The dot-dashed lines from the 'If' block denote control signals, which are used to enable If/Else (or other conditional) subsystems. Checking any of the boxes on the GUI produces a plot of any of the selected variables (versus time).
Limitations
The If block has the following limitations:
It does not support tunable parameters. Values for an
if
orelseif
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 Organize Parameter Data into a Structure by Using Struct Storage Class (Embedded Coder).
The if expression and elseif expressions cannot accept certain operators, such as
+
,-
,*
, and/
.
Ports
Input
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 Select Subsystem Execution.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| Boolean
Output
Outputs from the if
, else
, and
elseif
ports are action signals to If Action
Subsystem blocks.
Parameters
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
, wheren
equals the number of inputs that you specify.
Programmatic Use
Block Parameter:
NumInputs |
Type: character vector |
Values: '1'
| integer in quotes |
Default:
'1' |
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' |
Since R2025a
Select this parameter to ensure only if-else statements are included in the code generated from an If block.
- on
Only if-else statements are included in the generated code. This option prevents inclusion of switch-case statements in the generated code.
- off
When this parameter is off, the code generated from an If block might contain switch-case statements, which perform the same operations as the If block.
Programmatic Use
To set the block parameter value programmatically, use
the set_param
function.
Block Parameter:
EnsureIfElseCode |
Type: character vector |
Values:
'on' | 'off' |
Default:
'off' |
The If Action Subsystem block attached to the
if port executes when the associated if
expression evaluates to true
.
u1 > 0
Specify sending an action signal on the output port when the input
u1
is greater than0
.- logical expression
Note
Starting in R2025a, to edit the default if expression (
u1 > 0
), use the Expressions table. For a complex expression with multiple inputs and operators, select the if expression and edit it in the Expression editor box.For versions prior to R2025a, this parameter appears as If expressions in the block dialog box. You can specify logical expressions using this parameter.
However, across all Simulink® versions, this expression appears on the If block adjacent to the if output port.
The expression can include only the operators
<, <=, ==, ~=, >, >=, &, |, ~, ()
, andunary-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 anything other thandouble
orsingle
.
Programmatic Use
Block Parameter:
IfExpression |
Type: character vector |
Values: 'u1 >
0' | logical expression in quotes |
Default: 'u1 >
0' |
The If Action Subsystem block attached to an
elseif port executes when the associated
elseif
expression evaluates to
true
and all other if
and
elseif
expressions are false.
- empty
Logical expressions not specified.
- list of logical expressions
Note
Starting in R2025a, to add and edit elseif expressions, use the Expressions table. Use the action buttons on the left of the table to add, delete, and move the expressions. The table allows you to write each elseif expression separately instead of in a comma-separated list and organize the expressions per your modeling requirements. For a complex expression with multiple inputs and operators, select the row containing that elseif expression and edit it in the Expression editor box.
For versions prior to R2025a, this parameter appears as Else expressions in the block dialog box. In those versions, specify a list of logical expressions separated by commas.
However, across all Simulink versions, 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
<, <=, ==, ~=, >, >=, &, |, ~, ()
, andunary-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 anything other thandouble
orsingle
.
Programmatic Use
Block Parameter:
ElseIfExpressions |
Type: character vector |
Values: '' |
list of logical expressions separated by commas placed in
quotes |
Default:
'' |
Block Characteristics
Data Types |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
HDL Coder™ provides additional configuration options that affect HDL implementation and synthesized logic.
This block has one default HDL architecture.
ConstrainedOutputPipeline | Number of registers to place at
the outputs by moving existing delays within your design. Distributed
pipelining does not redistribute these registers. The default is
|
InputPipeline | Number of input pipeline stages
to insert in the generated code. Distributed pipelining and constrained
output pipelining can move these registers. The default is
|
OutputPipeline | Number of output pipeline stages
to insert in the generated code. Distributed pipelining and constrained
output pipelining can move these registers. The default is
|
Version History
Introduced before R2006a
The updated block dialog box allows you to specify the logical expressions in a table. You no longer need to specify the elseif expressions in a comma-separated list. This change makes the overall logic flow easier to visualize and parse. The table also contains new action buttons that allow you to add, delete, and move if-else expressions up and down. These buttons help you to organize the expressions per your modeling requirements. Additionally, the new expression editor box allows you to edit complex expressions.
Generated code contains if-else expressions. This change prevents inclusion of switch-case statements and enhances readability of the generated code.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)