This example explains how to convert a Variant Subsystem block to a Variant Assembly Subsystem block using the Simulink.VariantUtils.convertToVariantAssemblySubsystem
method. The Variant Assembly Subsystem block has the same variant choices as the Variant Subsystem block. However, the subsystem choices are converted to Subsystem Reference choices and saved as subsystem files during block conversion.
1. Consider the Controller
block in the slexVariantSubsystems
model. The Controller
block is a Variant Subsystem block that is to be converted to a Variant Assembly Subsystem block.
modelName = 'slexVariantSubsystems';
open_system(modelName);
vssBlockPath = [modelName,'/Controller'];
2. Set the variant control mode of the Controller
block to expression
.
set_param(vssBlockPath,'VariantControlMode','expression');
3. To convert the Controller
block to a Variant Assembly Subsystem block, use the convertToVariantAssemblySubsystem
method. During the conversion,
The subsystem choices Linear Controller
and Nonlinear Controller
are converted to Subsystem Reference blocks because the Variant Assembly Subsystem block does not support subsystem choices. The name of the Subsystem Reference blocks are the same as the subsystem blocks and are saved in the subsystem files Linear_Controller.slx and Nonlinear_Controller.slx
in the folder that you specify. For more information on how Simulink converts the subsystem blocks to Subsystem Reference blocks, see Convert an Existing Subsystem to a Referenced Subsystem.
An enumeration class controllerClass
is created in the folder that you specify. The controllerClass
class has Linear_Controller
and Nonlinear_Controller
choices as its members.
You may need to resolve any issues with the folders you specify for the enumeration class and subsystem files.
If either folder does not exist in the file system, Simulink creates the folder by using the mkdir
command and then adds it to the MATLAB path.
If either folder contains files with the same names as the enumeration class or subsystem files, Simulink overwrites them.
vcvName = 'EngType';
enumClassFilePath = 'controllerClass.m';
Simulink.VariantUtils.convertToVariantAssemblySubsystem ...
(vssBlockPath,VariantControlVariableName=vcvName,EnumerationClassFilePath=enumClassFilePath,SubsystemFilesFolderPath=pwd);
After successfully converting the Controller
block to a Variant Assembly Subsystem block, observe the expressions in Variant control expression for each variant choice. Simulink generates the expressions with the Variant control variable on the left side and the members of the Variant choices enumeration on the right side. Both sides of the expression are related by ==
. When an expression evaluates to true
during simulation, the corresponding variant choice becomes active.
4. Set Linear_Controller
active by specifying the value of EngType
to controllerClass.Linear_Controller
and simulate the model. During simulation, the control expression EngType == controllerClass.Linear_Controller
evaluates to true
and Linear_Controller
becomes active.
EngType = controllerClass.Linear_Controller;
sim(modelName);
For information on how to use Variant Assembly Subsystem blocks, see Add or Remove Variant Choices of Variant Assembly Subsystem Blocks Using External Files.