Documentation

Model Reference Variants

This example shows how to use model reference variants. A Model block is used to reference one Simulink® model from another Simulink model. A Variant Subsystem block can contain Model blocks as variants. A variant describes one of N possible modes a Variant Subsystem block can operate in. Each variant references a specific model with its associated model-specific arguments. Only one variant is active for simulation. You can switch the active variant by changing the values of variables in the base workspace, or by manually overriding variant selection using the Variant Subsystem block dialog.

For new models, use a Variant Subsystem block instead of a Model block to contain model variants, unless you need to use variants that are conditionally executed models (models with control ports). Support for using a Model block to contain model variants will be removed in a future release.

Example Requirements

During this example, Simulink and Simulink® Coder™ may generate code in the code generation folder in the current working folder. If you do not want to (or cannot) generate files in this folder, you should change the working folder.

Overview of Model Variants

A Model block is used to reference one Simulink model (the child model) from another Simulink model (the parent model). A Variant Subsystem block can have Model blocks as variants. The variants comprise a set of models that have the potential to be referenced by the Variant Subsystem block. In this example, there are two models that are potentially referenced by the Variant Subsystem block named Controller.

Each variant is associated with a Simulink.Variant object. Simulink.Variant objects are created in the MATLAB® base workspace. For example:

V_LINEAR_CONTROLLER=Simulink.Variant('CTRL==1');

where the constructor argument ('CTRL==1') defines the condition when the variant is active. In this case, defining:

CTRL=1

in the base workspace would activate the V_LINEAR_CONTROLLER variant. The condition argument can be a simple expression consisting of scalar variables, enumerations, equality, inequality, &&, , and ~. Parenthesis () can be used for precedence grouping.

For a given Variant Subsystem block, one variant is active during simulation. The active variant is determined by evaluating the variant object conditions in the MATLAB base workspace. Alternatively, you can instruct the Variant Subsystem block to use a specific variant by selecting the Override variant conditions and use following variant checkbox.

Specifying Variants

The model used in this example requires the following variables be defined in the MATLAB base workspace:

V_LINEAR_CONTROLLER=Simulink.Variant('CTRL==1');

V_NONLINEAR_CONTROLLER=Simulink.Variant('CTRL==2');

CTRL=2;

Opening the model sldemo_mdlref_variants runs the PreLoadFcn defined in File -> ModelProperties -> Callbacks. This callback populates the base workspace with the variables for the Variant Subsystem block named Controller:

Figure 1: The top model, sldemo_mdlref_variants

Right-click the Variant Subsystem block Controller and select the menu item Block Parameters (Subsystem) to open the block dialog box.

The dialog box specifies two potential variants. The two variants are in turn associated with the two Simulink.Variant objects V_LINEAR_CONTROLLER and V_NONLINEAR_CONTROLLER, which exist in the base workspace. These objects have a property named Condition, an expression that evaluates to a boolean and that determines which variant is active. The Condition is also shown in the Variant Subsystem block dialog. In this example, the condition of V_LINEAR_CONTROLLER and V_NONLINEAR_CONTROLLER are CTRL == 1 and CTRL == 2, respectively. The variable CTRL resides in the base workspace, and may be a standard MATLAB variable or a Simulink.Parameter.

Switching Active Variants

To simulate using the sldemo_mrv_linear_controller, define:

CTRL=1

in the base workspace and then simulate the model.

Figure 2: Simulation using the sldemo_mrv_linear_controller variant

To simulate using the sldemo_nonlinear_controller, define

CTRL=2

in the base workspace and then simulate the model.

Figure 3: Simulation using the sldemo_mrv_nonlinear_controller variant

Enumerations and Reuse

The sldemo_mdlref_variants_enum model illustrates Simulink.Variant capabilities:

1. Enumerations: MATLAB enumeration classes can be used to improve readability in the conditions of the variant object.

2. Reuse: Simulink.Variant objects can be reused in different Variant Subsystem blocks.

This example requires the following variables be defined in the MATLAB base workspace:

VE_LINEAR_CONTROLLER=Simulink.Variant('E_CTRL==sldemo_mrv_CONTROLLER_TYPE.LINEAR')

VE_NONLINEAR_CONTROLLER=Simulink.Variant('E_CTRL==sldemo_mrv_CONTROLLER_TYPE.NONLINEAR')

E_CTRL=sldemo_mrv_CONTROLLER_TYPE.LINEAR

VE_PROTOTYPE=Simulink.Variant('E_CURRENT_BUILD==sldemo_mrv_BUILD_TYPE.PROTOTYPE')

VE_PRODUCTION=Simulink.Variant('E_CURRENT_BUILD==sldemo_mrv_BUILD_TYPE.PRODUCTION')

E_CURRENT_BUILD=sldemo_mrv_BUILD_TYPE.PRODUCTION

In these Simulink.Variant objects we use the enumeration classes sldemo_mrv_BUILD_TYPE.m and sldemo_mrv_CONTROLLER_TYPE.m within the Simulink.Variant Condition properties to improve readability.

The VE_PROTOTYPE and VE_PRODUCTION Simulink.Variant objects are reused across the three filter Variant Subsystem blocks, Filter1, Filter2, and Filter3.

Opening the model sldemo_mdlref_variants_enum runs the PreLoadFcn defined in File -> ModelProperties -> Callbacks. This callback populates the base workspace with variables for the Variant Subsystem blocks, which are displayed in the MATLAB Command Window:

Was this topic helpful?