Main Content

Convert Variant Subsystem to Variant Assembly Subsystem

A Variant Assembly Subsystem block enables you to add or remove its variant choices from external sources without modifying the model. The Variant Assembly Subsystem block can contain Model blocks, Create and Use Referenced Subsystems in Models blocks, or both as its variant choices. The choices are maintained in corresponding model or subsystem files that are on the MATLAB® path.

Convert Variant Subsystem Block with Expressions to Variant Assembly Subsystem Block Interactively

This example shows how to convert a Variant Subsystem block that is in expression mode to a Variant Assembly Subsystem block through the Block Parameters dialog box. 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);
VSS_LINEAR_CONTROLLER = 
  VariantExpression with properties:

    Condition: 'VSS_MODE==1'

VSS_NONLINEAR_CONTROLLER = 
  VariantExpression with properties:

    Condition: 'VSS_MODE==2'

VSS_MODE = 2

2. Open the Block Parameters dialog box of the Controller block. From the Variant control mode list, select expression.

3. Click the Convert to Variant Assembly button.

4. In the Convert Variant Subsystem to Variant Assembly Subsystem dialog box, specify these properties for the converted block.

  • Name of the variant control variable — Specify a valid variable name that adheres to the rules in Valid Names or specify a valid structure field name that adheres to the rules in Generate Field Names from Variables. During the conversion, Simulink® creates the specified variable or the specified structure field to use as a variant control variable. The value of this variant control variable determines the active choice of the converted block during simulation.

  • Name of the enumeration class to be created — Specify a valid enumeration class name. During the conversion, Simulink creates the specified enumeration class with the Linear_Controller and Nonlinear_Controller choices as the members of the class. You can specify a folder to contain the enumeration class by clicking the Browse button. After specifying the enumeration class, the Absolute path to the folder of the enumeration file parameter displays the absolute path to the folder of that enumeration class.

5. During the conversion process, Simulink validates the types of variant choices in the Controller block. The Model and Subsystem Reference choices do not change. The Subsystem choices are converted to Subsystem Reference choices and are saved as subsystem files. The names of the newly created subsystem files are the same as the Subsystem choices. In this example, the Subsystem choices Linear Controller and Nonlinear Controller are converted to Subsystem Reference choices and are saved in the subsystem files Linear_Controller.slx and Nonlinear_Controller.slx. For more information on how Simulink converts the subsystem blocks to Subsystem Reference blocks, see Convert Subsystem to a Referenced Subsystem.

These parameters display the details of the subsystem files:

  • List of new subsystem filenames — Lists the names of the subsystem files to be created.

  • Absolute path to the folder for new subsystem files — Displays the absolute path to the folder of the subsystem files to be created.

  • Location of new subsystem files — Displays the name of the folder where the subsystem files are to be created. To change the location of the subsystem files, specify a path relative to your current working folder or specify an absolute path. You can also select a folder from the file system by clicking the Browse folder button.

6. The Variant choices (table of variant systems) table lists the names of the variant choices in the converted block. For each variant choice, Simulink generates a Variant control expression. The variant control variable is on the left side and the members of the variant enumeration class are on the right side of the generated expressions. Both sides of the expression are related by ==. When an expression evaluates to true during simulation, the corresponding variant choice becomes active.

7. To validate the current configuration of the Controller block, click Convert. If these validations pass, the Controller block is converted to a Variant Assembly Subsystem block. You may need to resolve any issues with the folders you specify for the enumeration class and subsystem files.

  • If either folder exists in the file system but not on the MATLAB path, you must resolve the issue by clicking Add to path or by using the addpath function.

  • 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, you must determine if you want to replace them.

8. To simulate the model for Linear_Controller, specify the value of myVCV to myEnum.Linear_Controller and simulate the model. During simulation, the control expression myVCV == myEnum.Linear_Controller evaluates to true and Linear_Controller becomes active.

For information on how to use the Variant Assembly Subsystem block, see Add or Remove Variant Choices of Variant Assembly Subsystem Blocks Using External Files.

Convert Variant Subsystem Block with Expressions to Variant Assembly Subsystem Block Programmatically

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);
VSS_LINEAR_CONTROLLER = 
  VariantExpression with properties:

    Condition: 'VSS_MODE==1'

VSS_NONLINEAR_CONTROLLER = 
  VariantExpression with properties:

    Condition: 'VSS_MODE==2'

VSS_MODE = 2
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.

Convert Variant Subsystem Block with Labels to Variant Assembly Subsystem Block Interactively

This example shows how to convert a Variant Subsystem block that is in label mode to a Variant Assembly Subsystem block through Block Parameters dialog box. 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);
VSS_LINEAR_CONTROLLER = 
  VariantExpression with properties:

    Condition: 'VSS_MODE==1'

VSS_NONLINEAR_CONTROLLER = 
  VariantExpression with properties:

    Condition: 'VSS_MODE==2'

VSS_MODE = 2

2. Open the Block Parameters dialog box of the Controller block. From the Variant control mode list, select label.

3. Click the Convert to Variant Assembly button.

4. During the conversion process, Simulink validates the types of variant choices in the Controller block. The Model and Subsystem Reference choices do not change. The Subsystem choices are converted to Subsystem Reference choices and are saved as subsystem files. The names of the newly created subsystem files are the same as the Subsystem choices. In this example, the Subsystem choices Linear Controller and Nonlinear Controller are converted to Subsystem Reference choices and are saved in the subsystem files Linear_Controller.slx and Nonlinear_Controller.slx. For more information on how Simulink converts the subsystem blocks to Subsystem Reference blocks, see Convert Subsystem to a Referenced Subsystem.

These parameters display the details of the subsystem files:

  • List of new subsystem filenames — Lists the names of the subsystem files to be created.

  • Absolute path to the folder for new subsystem files — Displays the absolute path to the folder of the subsystem files to be created.

  • Location of new subsystem files — Displays the name of the folder where the subsystem files are to be created. To change the location of the subsystem files, specify a path relative to your current working folder or specify an absolute path. You can also select a folder from the file system by clicking the Browse folder button.

5. The Variant choices (table of variant systems) table lists the names of the variant choices in the converted block. For each variant choice, Simulink generates a Variant control label. The variant choice that corresponds to the control label selected in Label mode active choice of the converted block becomes active during simulation or code generation.

6. To validate the current configuration of the Controller block, click Convert. If these validations pass, the Controller block is converted to a Variant Assembly Subsystem block:

  • If the specified folder exists in the file system but not on the MATLAB path, you must resolve the issue by clicking Add to path or by using the addpath function.

  • If the specified folder does not exist in the file system, Simulink creates the folder by using mkdir command and adds it to the MATLAB path.

  • If the specified folder contains a file with the same name as one of the new subsystem files, you must determine if you want to replace it.

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.

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

Convert Variant Subsystem Block with Labels to Variant Assembly Subsystem Block Programmatically

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);
VSS_LINEAR_CONTROLLER = 
  VariantExpression with properties:

    Condition: 'VSS_MODE==1'

VSS_NONLINEAR_CONTROLLER = 
  VariantExpression with properties:

    Condition: 'VSS_MODE==2'

VSS_MODE = 2
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);

See Also