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 in which a Variant Subsystem block can operate. Each variant references a specific model with its associated model-specific arguments. For a given Variant Subsystem block, only one variant is active during 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 box.
sldemo_mdlref_variants model contains a Variant Subsystem block named Controller that references two models.
To see the variant choices, right-click the Variant Subsystem block named Controller and select Block Parameters (Subsystem). The active variant is determined by the value of the
CTRL variable in the MATLAB® base workspace. Defining
CTRL=1 activates the Model block named Linear, and defining
CTRL=2 activates the Model block named Nonlinear.
sldemo_mdlref_variants model runs the PreLoadFcn model callback, which sets
To simulate using the linear controller, define
CTRL=1 in the base workspace, then simulate the model.
To simulate using the nonlinear controller, define
CTRL=2 in the base workspace, then simulate the model.
sldemo_mdlref_variants_enum model demonstrates
Simulink.Variant object capabilities:
Enumerations: MATLAB enumeration classes can be used to improve the readability of the conditions of the
Variant objects can be reused in different Variant Subsystem blocks.
The Variant Subsystem blocks in this model require 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
Each variant is associated with a
Simulink.Variant object that uses either the
sldemo_mrv_CONTROLLER_TYPE.m enumeration class.
Variant objects have a
Condition property that is an expression. The expression evaluates to a Boolean and determines which variant is active. The expression can consist of scalar variables, enumerations, equality, inequality, &&, ||, and ~. Parenthesis () can be used for precedence grouping. Variables may be standard MATLAB variables or
Opening the model
sldemo_mdlref_variants_enum runs the PreLoadFcn model callback. This callback is set up to populate the base workspace with the variables required by the Variant Subsystem blocks.
Variant objects are reused across the Variant Subsystem blocks named Filter1, Filter2, and Filter3.
The Block Parameters dialog box of the Variant Subsystem blocks show the Condition property of the related