Variant Control Modes in Variant Blocks - MATLAB & Simulink (original) (raw)

The components of a Simulink® model that contain variants are activated or deactivated based on the variant choice that you select.

Each variant choice in your model is associated with a variant control. Variant controls determine which variant choice is active. By changing the value of a variant control, you can switch the active variant choice. While each variant choice is associated with a variant control, only one variant control can evaluate totrue. When a variant control evaluates to true, Simulink activates the variant choice that corresponds to that variant control. For a simple example, see Introduction to Variant Controls.

Types of Variant Control Modes in Variant Blocks

These are types of variant control modes in Simulink:

Types of variant control modes with examples. In expression mode, variant control can be a boolean condition expression, a Simulink.VariantExpression object, or a default variant choice. In label mode, variant control is a string, and in sim codegen switching mode, variant controls are (sim) and (codegen) keywords.

Switch Between Choices Using Condition Expressions in Variant Blocks

In expression mode, Simulink chooses the active variant based on the evaluation of the variant conditions. When a condition expression evaluates to true, the corresponding variant choice becomes active. When a condition expression evaluates to false, the corresponding variant choice becomes inactive.

You can use the expression type of variant controls in variant blocks and in variant parameters.

Note

You can simulate and generate code from the model containing a variant block withVariant control mode set to expression mode. The generated code can contain active and inactive choices that are enclosed in preprocessor conditionals #if and #elif, or regular if conditions.

Use expression Type of Variant Control in Variant Blocks

To specify the variant condition expressions in variant blocks:

Types of Variant Controls in expression Mode

The variant controls can be:

Here, A and B are operands called as_variant control variables_. +,==, and && are operators in the condition expression. The condition expression can contain one or more such variant control variables and operators. For information on supported types and storage location of variant control variables, see Types of Variant Control Variables (Operands) in Variant Blocks and Storage Locations for Variant Control Variables (Operands) in Variant Blocks. For information on operators, see Types of Operators in Variant Blocks for Different Activation Times.

Note

You can comment out the variant controls by placing a % symbol before the control expressions. During simulation, if the corresponding variant choice is active, Simulink ignores the variant choice. However, Simulink continues to execute block callbacks inside the variant choice.

Types of Variant Control Variables (Operands) in Variant Blocks

In expression mode, you can specify the variant control variables as any of these types, from options to use while prototyping to options required for generating code from your model.

For list of all examples, see Use Variant Control Variables in Variant Blocks.

Storage Locations for Variant Control Variables (Operands) in Variant Blocks

You can define the variant control variables in different storage locations based on your requirement. When you define a variable across the base, mask, and model workspaces, the evaluation order begins with the mask workspace, followed by the model workspace, and then the base workspace. This means that the variable's definition in the closest context is prioritized, ensuring that the most relevant value is used for evaluating the variant condition expressions.

Note

Storage Location Use for Storage Location Supported Types of Variant Control Variables (See Types of Variant Control Variables (Operands) in Variant Blocks) For More Information
Base workspace Store variables while you experiment with temporary models Scalar variables, Simulink.VariantExpression,Simulink.Parameter, enumerated type,Simulink.VariantControl, andstruct Temporary Data: Base Workspace
Mask workspace Permanently store data that is local to the mask of a block. Scalar variables, enumerated type, andSimulink.VariantControl Approaches to Control Active Variant Choice of a Variant Block Using Mask or Model Workspace
Model workspace Permanently store data that is local to a model Scalar variables, enumerated type, andSimulink.VariantControl Approaches to Control Active Variant Choice of a Variant Block Using Mask or Model Workspace
Data dictionary Permanently store global data, share data between models, and track changes made to data. Scalar variables, Simulink.VariantExpression,Simulink.Parameter, enumerated type,Simulink.VariantControl, andstruct What Is a Data Dictionary?

For capabilities and advantages of each storage location, see Determine Where to Store Variables and Objects for Simulink Models

Types of Operators in Variant Blocks for Different Activation Times

Variant condition expressions can contain MATLAB® operators, provided the expression evaluates to a Boolean value.

In variant blocks, the operators that you can use to form a variant condition expression depends on the Variant activation time.

When you specify the Variant activation time as:

Note

In variant blocks with startup activation time:

Evaluate Variant Condition Expressions at Different Variant Activation Times

In expression mode, you can specify if Simulink must evaluate condition expressions during model compile, simulation-loop, code compile, or model start up stage of simulation and code generation workflow using variant activation times. For information on stages of simulation and code generation and supported variant activation times in expression mode, seeActivate Variant During Different Stages of Simulation and Code Generation Workflow.

Automatically Identify Variant Regions Using Variant Condition Propagation

Simulink determines the model components that are active during simulation by the process of variant condition propagation. This process evaluates the variant controls specified on the variant blocks and automatically propagates the variant conditions to the connecting blocks. Variant conditions can propagate through signal lines, buses, and function calls to reach other blocks in the model. The process deactivates the model components associated with the inactive choices and they do not participate in simulation. You can stop condition propagation to define variant regions in the model.

You can limit variant condition propagation in a bounded region using theVariant Start and Variant End blocks. For more information on bounded region, see Variant Start.

You can use the Variant Conditions Legend to visualize the propagated variant conditions that activate each variant choice. Simulink annotates model components if there are variant conditions on them, and the Variant Conditions Legend displays the condition that corresponds to each annotation.

For more information, see Propagate Variant Conditions to Define Variant Regions with Variant Blocks.

Switch Between Choices Using Labels in Variant Blocks

In label mode, Simulink chooses the active variant based on the name of the variant. The variant control is a string and does not require you to create variable in any workspaces.

Use label Mode in Variant Blocks

To specify the labels for choices in variant blocks, right-click the badge on the variant block, select , and then specify the labels in the Variant control label parameter of the block parameter dialog box.

Variant control mode: label

Variant Subsystem block parameter dialog box with Variant control mode set to label

Set Active Choices Using Variant Control Labels

You can follow any of these approaches to set active choices inlabel mode:

Switch Between Choices for Simulation and Code Generation Workflows Without Using Control Variables in Variant Blocks

To automatically switch between the choices for simulation and code generation workflows without creating any workspace variable, use the sim codegen switching variant control mode.

Use sim codegen switching Mode in Variant Blocks

To specify simulation and code generation branches for variant choices, right-click the badge on the variant block, select , and then specify the (sim) and (codegen) keywords in theVariant control switch parameter of the block parameter dialog box.

When you simulate a model in normal, accelerator, or rapid Accelerator mode, Simulink automatically chooses the (sim) branch as the active choice. Similarly, when you do a software-in-the-loop (SIL), processor-In-Loop (PIL) simulation or generate code or use external mode, Simulink automatically chooses the (codegen) branch. The code is generated only for the active choice. Inactive choices are not included in the generated code. In this mode, you can at most have only two choices in the variant block.

Variant control mode: sim codegen switching

Variant Subsystem block parameter dialog box with Variant control mode set to sim codegen switching

Compare Different Types of Variant Control Modes in Variant Blocks

This table explains the capabilities of different variant control modes to help you choose one that caters to your requirement.

Variant control mode Capabilities
expression — Simulink chooses the active variant based on the evaluation of the variant conditions. When a condition expression evaluates to true, the corresponding variant choice becomes active. When a condition expression evaluates to false, the corresponding variant choice becomes inactive. Use expression mode to: Control the active choice from different sources such as base, mask, model workspace, and data dictionary. Each source defines a scope of visibility, which allows you to efficiently set different active choices for blocks in different scope. For more information, see Storage Locations for Variant Control Variables (Operands) in Variant Blocks.Set different active choices for multiple instances of a variant block that belongs to a custom locked library. For more information, see Control Active Choice of Locked Custom Library Variant Subsystem Using Mask Parameter.Propagate the state of the underlying blocks outside of Variant Subsystem to improve the accuracy of the model and to simplify model construction. You are not required to manually compute the variant conditions assigned to the dependent or connected blocks. For more information, see Propagate conditions outside of variant subsystem.Use code compile activation time to:Generate a code that contains active and inactive choices of the model in C preprocessor conditionals #if and#elif. The preprocessor conditionals are conditionally compiled for the active choice that you provide as an input to the compiler. For more information, see Compile Code Conditionally for Variations of Component Represented Using Variant Block (Embedded Coder).Generate conditionally compiling code for AUTOSAR targets.Identify incorrect model constructs, detect design errors, and generate test cases for model coverage of active variants. For more information, see Verify and Validate Variant Models with Code Compile Activation Time.Use startup activation time to: Improve the speed of iterative simulations and Simulink Test workflows using fast restart. For more information, see Run Iterative Simulations Without Recompiling Model for Variant Systems Using Fast Restart.Simulate multiple configurations in parallel with different active choices in each run. For more information, see Simulate Variant Subsystem with Startup Activation Using parsim.Identify incorrect model constructs, detect design errors, and generate test cases for model coverage of active and inactive variants. For more information, see Verify and Validate Variant Models with Startup Activation Time. Generate a code that contains active and inactive choices of the model in regular if conditions. Theif condition is evaluated and conditionally executed for the active choice that you provide as an input to the executable. For more information, see Run Executables for Variant Blocks Without Recompiling Code for Changing Active Choices Using Startup Activation Time (Simulink Coder).For AUTOSAR variants, include post-build variation points in ARXML files. For more information, see Configure Postbuild Variant Conditions for AUTOSAR Software Components (AUTOSAR Blockset).Use runtime activation time to: Switch active variants by using the Parameter Writer inside a conditionally executed subsystem and an event function. For more information, see Switch Active Variants at Run Time Using a Parameter Writer Block Inside a Conditional Subsystem and Switch Active Variants at Run Time Using a Parameter Writer Block Inside an Event Function.Switch active choice during execution of the generated code. For more information, see Code Generation with Run-time Variants.Control run-time variant blocks using model arguments and system mask.Explicitly model the state hand-off when you switch from active choice to inactive or from inactive choice to active by using theState Reader and State Writer blocks.Generate coverage aggregation for the variant choices depending on whether they are active or not and whether the decision coverage criteria is met using Simulink Coverage™.Inject noise in all variant choices simultaneously without exiting the simulation by using Simulink Fault Analyzer™.
label — Simulink chooses the active variant based on the name of the variant. The variant control is a string and does not require you to create variable in any workspace. Use label mode to: Control active choices without creating any workspace variables.Generate code only for active choice in the model.
sim codegen switching — When you simulate a model in normal, accelerator, or rapid Accelerator mode, then Simulink automatically chooses the sim branch as the active choice. Similarly, when you do a software-in-the-loop (SIL), processor-In-Loop (PIL) simulation or generate code or use external mode, Simulink automatically chooses the codegen branch. Use sim codegen switching switching mode to: To automatically switch between the choices for simulation and code generation workflows without creating any workspace variables.Generate code only for active choice in the model.

Variant Activation Times for Different Variant Control Modes in Variant Blocks

This table explains the variant activation time supported for different variant control modes in variant blocks. For information on variant activation times, see Activate Variant During Different Stages of Simulation and Code Generation Workflow.

See Also

Topics