Control Active Choice of Locked Custom Library Variant Subsystem Using Mask Parameter
This example shows how to control the active choice of a Variant Subsystem that belongs to a locked custom library by using a mask parameter as Variant control variable. Mask parameters limit the scope of the variable, which allows you to use the same names for control variables in different scopes. This example includes promoted parameters and control expressions with enumeration classes to simplify the interface of the mask dialog and control expressions with enumeration classes to improve the readability of the variant condition expressions. For more information on Variant Subsystems, see Implement Variations in Separate Hierarchy Using Variant Subsystems.
Model
Open the Simulink® model by typing the following command in the MATLAB® Command Window :
openExample("simulink_variants/SimulinkVariantsExample",... "supportingFile","slexVariantSubsystemUsingMaskAndEnums.slx")
Consider the Engine subsystem block in the locked custom library,
slexVarEngineLibrary
.
The mask dialog box of the subsystem contains these parameters:
Engine type: When you select a value for this parameter, Simulink assigns the index of that value to the mask parameter
engine
. During simulation, the value ofengine
is used to evaluate the variant condition expressions to activate or deactivate the underlying Turbo Engine and Non Turbo Engine subsystems.Fidelity type for turbo engine: This parameter becomes available only if the Engine type parameter is set to
Turbo
. This parameter is promoted on to the mask dialog of the Engine subsystem from the underlying layer of the Turbo Engine subsystem. When you select a value for this parameter, Simulink assigns the index of that value to the mask parameterenginetype
of the Turbo Engine subsystem. During simulation, the value ofenginetype
is used to evaluate the variant condition expression to activate or deactivate the underlying High, Medium, and Low subsystems.Fidelity type for non turbo engine: This parameter becomes available only if the Engine type parameter is set to
Non Turbo
. This parameter is promoted on to the mask dialog of the Engine subsystem from the underlying layer of the Non Turbo Engine subsystem. When you select a value for this parameter, Simulink assigns the index of that value to the mask parameterenginetype
of the Non Turbo Engine subsystem. During simulation, the value ofenginetype
is used to evaluate the variant condition expression to activate or deactivate the underlying High, Medium, and Low subsystems.
The scope of enginetype
in the Turbo Engine subsystem is different
from the scope of enginetype
in the Non Turbo Engine subsystem.
enginetype
of Turbo Engine is visible only to the underlying layers of
the Turbo Engine subsystem. Similarly, enginetype
of Non Turbo Engine is
visible only to the underlying layers of the Non Turbo Engine subsystem. Limiting the scope
by using mask parameters as Variant control variables allows you to use the same name for
variables with holding different values in the Turbo Engine and the Non Turbo Engine
subsystems.
Switch Between Active Choices
To simulate the model, on the Simulation tab, click Run. On the mask dialog of the Engine subsystem, the Engine type parameter is set to
Non Turbo
, and the Fidelity type for non turbo engine is set toMedium
. As these parameters are mapped to the index of the mask parametersengine
andengineType
, the value ofengine
is set to2
, and the value ofengineType
is set to1
. Here,2
specifies the index of theNon Turbo
option, and1
specifies the index of theHigh
option. For more information on how the parameters on the mask dialog box are mapped to the underlying mask parameters, see Create a Simple Mask.During simulation, the condition expressions
engine == Engine.NONTURBO
andengineType == EngineFidelity.MEDIUM
evaluate totrue
. Here,Engine
andEngineFidelity
are integer-based enumeration classes defined inEngine.m
andEngineFidelity.m
files with values forEngine.TURBO
andEngine.NONTURBO
specified as1
and2
, and the values forEngineFidelity.HIGH
,EngineFidelity.MEDIUM
, andEngineFidelity.LOW
specified as1
,2
, and3
.The NonTurbo Engine subsystem becomes active and the Turbo Engine subsystem becomes inactive.
To modify the active choice, select
Turbo
in the mask dialog box, then simulate the model again. During simulation, the value ofengine
is set to1
, which evaluates the variant conditionengine == Engine.TURBO
totrue
. The Turbo Engine subsystem becomes active, and the Non Turbo Engine subsystem becomes inactive.