Main Content

Create a Simple Variant Model

This example shows you how to create a simple model with variant blocks.

Variant blocks provide you a way to design alternatives of a system in a single model. Each design choice is incorporated into the model as a variant choice in a single layer or a separate hierarchy. Each variant choice is associated with a variant control. During simulation, the variant choices are activated or deactivated depending on the variant controls you specify.

Use variant blocks to switch among blocks for multiple simulation, code generation, or testing workflows.

Model Description

Open the slexVariantSubsystems model that contains a Variant Subsystem block.

open_system('slexVariantSubsystems')

Define Variant Control for Variant Subsystems

This section explains how to define variant control variables that determine the condition under which a variant choice is active.

1. Right-click the variant block and click Block Parameters.

2. To choose the active Variant based on the evaluation of the Variant conditions, use the expression mode, else select label mode. When you select the Variant control mode as label, the Label mode active choice option is available. In label mode, Variant control need not be created in the base workspace or data dictionary used by the model. You can select an active Variant choice from Label mode active choice options.

3. Use the options available on the Block Parameter dialog box to add variant controls and its corresponding variant condition.

A sample screenshot for expression mode:

Note: The variables used to specify the variant control and variant condition must be defined in the base workspace, model workspace, mask workspace or data dictionary for the model.

4. Specify a default variant condition to be used when there is no active variant choice. Use the Variant control list to specify the default.

5. To activate a variant choice, type the variant choice in MATLAB® command window. For example, type VSS_MODE = 2.

6. To simulate the model, on the toolstrip, click Run. The model simulates for the specified active choice.

sim('slexVariantSubsystems');

7. Modify the active choice and simulate the model again, if necessary.

Generate Code for Variant Subsystems

This section explains how to generate code for the variants model with preprocessor conditionals. For detailed information, see Compile Code Conditionally for Variations of Component Represented Using Variant Block (Simulink Coder).

Note: You must have an Embedded Coder® license to generate the code.

1. In the Block Parameters dialog box, from the Variant activation time list, select code compile.

2. Open the Subsystem Block Parameters dialog boxes. Select the Treat as atomic unit parameter.

3. In the Code Generation section of Configuration Parameters dialog box, specify the System target file as ert.tlc.

4. In Model Explorer, define the variables used to specify the variant choice as a MATLAB variable or as a Simulink.Parameter. The data type of the Simulink.Parameter can be of type Integer, Boolean, or Enumerated and the storage class can be either importedDefine(Custom), Define(Custom), or CompilerFlag.

Export Control Variables to MAT-File

For the variants that are defined in the base workspace, you can export the control variables to a MAT-file. For example, type the following in the MATLAB command window:

  • save <MAT-File Name> <Variable Name>

  • PostLoadCallback > load <MAT-File Name>

Note: To update or refresh active models that contain Variant Subsystem blocks, on the Modeling tab, click Update Model (Ctrl + D) in Simulink.

See Also

| | |