Main Content

Simulink.VariantUtils.convertToVariantAssemblySubsystem

Class: Simulink.VariantUtils
Namespace: Simulink

Convert Variant Subsystem block to Variant Assembly Subsystem block

Since R2023b

Description

Simulink.VariantUtils.convertToVariantAssemblySubsystem(blockPathOrHandle) converts the Variant Subsystem block blockPathOrHandle to a Variant Assembly Subsystem block. Use this syntax if the variant control mode of the Variant Subsystem block is label and has only Model blocks and Create and Use Referenced Subsystems in Models blocks as its variant choices.

example

Simulink.VariantUtils.convertToVariantAssemblySubsystem(blockPathOrHandle,Name=Value) converts the Variant Subsystem block blockPathOrHandle to a Variant Assembly Subsystem block as specified by one or more Name-Value arguments. Use this syntax if the variant control mode of the Variant Subsystem block is expression or has at least one Subsystem block as its variant choice.

example

Input Arguments

expand all

Path or handle of the Variant Subsystem block to convert to a Variant Assembly Subsystem block, specified as a character vector or a string scalar (for a block path) or double scalar (for a handle).

Example: 'slexVariantSubsystem/Controller'

Data Types: char | string | double

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: Simulink.VariantUtils.convertToVariantAssemblySubsystem('slexVariantAssemblySubsystem/Controller', VariantControlVariableName='EngType', EnumerationClassFilePath='ControllerType.m', SubsystemFilesFolderPath='ControllerChoices') converts the Variant Subsystem block Controller to a Variant Assembly Subsystem block.

Valid MATLAB variable name or structure field name to be created, specified as a string scalar or a character vector. For the naming rules, see Variable Names and Generate Field Names from Variables.

During simulation, Simulink® uses the value of this parameter to determine the active variant choice of the converted Variant Assembly Subsystem block.

Example: 'EngType'

Dependencies

Use this argument if the VariantControlMode parameter of the Variant Subsystem block is set to 'expression'.

Data Types: char | string

Absolute or relative path to the MATLAB class file to be created, specified as a string scalar or a character vector. During the block conversion, Simulink defines an enumeration class in the specified enumeration class file with the variant choices of the converted Variant Assembly Subsystem block as its members.

Example: 'ControllerType.m'

Dependencies

Use this argument if the VariantControlMode parameter of the Variant Subsystem block is set to 'expression'.

Data Types: char | string

Absolute or relative folder path where the Subsystem Reference choices of the converted Variant Assembly Subsystem block must be saved as subsystem files, specified as a string scalar or a character vector.

Example: SubsystemFilesFolderPath = 'ControllerChoices'

Dependencies

Use this argument if the Variant Subsystem block has at least one Subsystem block as its variant choice.

Data Types: char | string

Examples

expand all

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 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.

This example explains how to convert a Variant Subsystem block to a Variant Assembly Subsystem 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 label.

set_param(vssBlockPath,'VariantControlMode','label');

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 newly created 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 Subsystem to a Referenced Subsystem.

You may need to resolve any issues with the folders you specify for the subsystem files.

  • If the specified folder does not exist in the file system, Simulink creates the folder by using mkdir command.

  • If the specified folder contains files with the same name as the new subsystem files, Simulink overwrites them.

Simulink.VariantUtils.convertToVariantAssemblySubsystem(vssBlockPath,SubsystemFilesFolderPath=pwd);

After successfully converting the Controller block to a Variant Assembly Subsystem block, observe the expression in the Variant choices specifier parameter. The expression is of the form {'Linear_Controller', 'Nonlinear_Controller'}, where Linear_Controller and Nonlinear_Controller are the variant choices of the converted block. To add or remove variant choices from the Controller block, change the specifier as described in Add or Remove Variant Choices of Variant Assembly Subsystem Blocks Using External Files.

4. To simulate the model for Linear_Controller, set the value of Label mode active choice to Linear_Controller and simulate the model.

set_param(vssBlockPath,'LabelModeActiveChoice','Linear_Controller');
sim(modelName);

Limitations

Only Variant Subsystem blocks with the Variant control mode set to label or expression can be converted to a Variant Assembly Subsystem block.

Alternatives

You can convert a Variant Subsystem block to a Variant Assembly Subsystem block through the block dialog. For more information, see Convert Variant Subsystem to Variant Assembly Subsystem.

Version History

Introduced in R2023b

expand all