Main Content

Add or Remove Variant Choices of Variant Assembly Subsystem Blocks Using External Files

A Variant Assembly Subsystem block can use Model blocks, Create and Use Referenced Subsystems in Models blocks, or both as its variant choices. The variant choices are maintained in a separate model and subsystem files that are on the MATLAB® path. You can add or remove the variant choices from the Variant Assembly Subsystem block without modifying the model. You use an external source such as an enumeration class or a MATLAB function to manage the variant choices of the Variant Assembly Subsystem block. Unlike when you use Variant Subsystem blocks, you are not required to navigate inside the Variant Assembly Subsystem block to add or remove the variant choices from the block. For more information, see Variant Assembly Subsystem.

This example explains how to:

Manipulate Variant Choices of Variant Assembly Subsystem Block in Label Mode

This section shows how to add or remove the variant choices of a Variant Assembly Subsystem block in label mode.

Maintain Variant Choices of Variant Assembly Subsystem Block in Model and Subsystem Files

A Variant Assembly Subsystem block can contain Model blocks, Create and Use Referenced Subsystems in Models blocks, or both as its variant choices. The variant choices are maintained in separate model and subsystem files that are on the MATLAB® path.

1. Open the slexVariantAssemblySubsystemInLabel model. The model contains a Variant Assembly Subsystem block Controller.

2. To add variant choices to the Controller block, open the Block Parameters dialog box and specify this MATLAB expression in the Variant choices specifier parameter.

{'linear_generic1','linear_generic2','nonlinear_generic1','nonlinear_generic2'}

In this example, the linear_generic1 and the linear_generic2 choices are Model blocks. The nonlinear_generic1 and nonlinear_generic2 blocks are Subsystem Reference blocks. These variant choices are maintained in their respective files inside the ControllersList folder. To create the ControllersList folder, run the createFolderStructureForVAS function.

createFolderStructureForVAS(pwd)

Ensure that the ControllersList folder and all its subfolders are added to the search path of your current MATLAB session by using the addpath function.

3. After you specify an expression in the Variant choices specifier parameter, click the Validate button. The expression is evaluated to ensure that it is a valid MATLAB expression that returns a one-dimensional cell array of model or subsystem file names or file name patterns that are on the MATLAB path. On successful validation, the variant choices are added to the Controller block. Any errors encountered during the validation are displayed in the block dialog.

4. Double-click the Controller block to observe the four variant choices linear_generic1, linear_generic2, nonlinear_generic1, and nonlinear_generic2 that are added to the Controller block. The file name and the path of the newly added variant choices are displayed in the Variant Choices table in the Reference tab.

5. From Label mode active choice, select linear_generic1 and simulate the model. Observe that the linear_generic1 choice becomes active.

6. You can modify the expression in the Variant choices specifier parameter as described in Specify Variant Choices of Variant Assembly Subsystem Block in Variant Choices Specifier Parameter, then simulate the model again.

Specify Variant Choices of Variant Assembly Subsystem Block in Variant Choices Specifier Parameter

To add Model or Subsystem Reference blocks as variant choices to a Variant Assembly Subsystem block, specify the names of the files to be referenced. The file names must be specified as a MATLAB expression that returns a one-dimensional cell array of character vectors when evaluated. You can then validate the expression using the Refresh button. On a successful validation, the blocks are added as variant choices to the Variant Assembly Subsystem block. For each choice, a variant control label is generated. The name of the variant control label is same as the name of the choice. The newly added variant choices and their corresponding control labels are displayed in the Variant choices table.

The MATLAB expression that you specify must contain either of these:

  • Model or subsystem file names that are on the search path of your current MATLAB session. For example, if you specify {'model1.slx', 'subsystemRef1.slx'} as a specifier, the blocks that reference the model1.slx and subsystemRef1.slx files are added as variant choices of the Variant Assembly Subsystem block.

  • File name patterns which match the model or subsystem file names on the MATLAB path. The paths in the pattern can be absolute or relative to the parent folder of the model that contains the Variant Assembly Subsystem block. For example, if you specify {'folder_1/*.slx', 'folder_2/*.slx'} as a specifier, Simulink® passes the character vectors folder_1/* and folder2/* to the dir function. The function then returns the model and the subsystem files that are located inside the folder1 and folder2 folders. The blocks that reference the returned files are added as variant choices of the Variant Assembly Subsystem block.

For information on how to add files or folders to the search path, see addpath.

The following table shows different ways in which you can specify the Variant choices specifier parameter in the Controller block. Specify these expressions in the Variant choices specifier parameter and validate them by clicking the Refresh button. On successful validation, the variant choices are added to the Controller block.

Variant Choices SpecifierAdded Variant Choices

Description — Specify a relative path to a folder that contains model or subsystem files.

Value{'ControllersList/*'}

The model or subsystem files that are located inside the ControllersList folder are added as variant choices. Files inside the subfolders of ControllersList are not added. The added choices are:

  • controller_generic1

  • controller_generic2

Description — Specify relative path to a folder and its subfolders that contain model or subsystem files.

Value{'ControllersList/**'}

The model or subsystem files that are located inside the ControllersList folder are added as variant choices. Files inside the subfolders of ControllersList are also added. The added choices are:

  • linear_groupA_type1

  • linear_groupA_type2

  • linear_groupB_type1

  • linear_groupB_type2

  • linear_generic1

  • linear_generic2

  • nonlinear_groupA_type1

  • nonlinear_groupA_type2

  • nonlinear_groupB_type1

  • nonlinear_groupB_type2

  • nonlinear_generic1

  • nonlinear_generic2

  • controller_generic1

  • controller_generic2

Description — Specify relative path to a folder and its subfolders one level down that contain model or subsystem files.

Value{'ControllersList/*', 'ControllersList/*/*'}

The model or subsystem files that are located inside the ControllersList folder are added as variant choices. Files inside the subfolders linear and nonlinear are also added. The added choices are:

  • linear_generic1

  • linear_generic2

  • nonlinear_generic1

  • nonlinear_generic2

  • controller_generic1

  • controller_generic2

Description — Specify relative path to a folder that contain model or subsystem files and has subfolder names with a certain ending.

Value{'ControllersList/**/*groupA/*'}

The model or subsystem files that are located inside the linear_groupA and nonlinear_groupA subfolders are added as variant choices. The added choices are:

  • linear_groupA_type1

  • linear_groupA_type2

  • nonlinear_groupA_type1

  • nonlinear_groupA_type2

Description — Specify an absolute path to a folder that contains model or subsystem files

Value — In Windows® systems, {'C:\VAS\ControllersList\*'}

In UNIX® systems, {'/VAS/ControllersList/*'}

The model or subsystem files that are located inside the ControllersList folder are added as variant choices. Files inside the subfolders of ControllersList are not added. The added choices are:

  • controller_generic1

  • controller_generic2

Description — Specify the model or subsystem file names in a cell array

Value{'linear_groupA_type1', 'linear_groupB_type2', 'nonlinear_generic1', 'controller_generic2'}

The specified model and subsystem files are added as variant choices. The added choices are:

  • linear_groupA_type1

  • linear_groupB_type2

  • nonlinear_generic1

  • controller_generic2

Description — Create a MATLAB workspace variable that returns a cell array of models or subsystem file names. You can then specify the variable name as a variant choices specifier.

For example, controllers:

controllers = {'linear_groupA_type1', 'linear_groupB_type2', ...
'nonlinear_generic1', 'controller_generic2'};

Valuecontrollers

The model or subsystem files specified in controllers are added as variant choices. The added choices are:

  • linear_groupA_type1

  • linear_groupB_type2

  • nonlinear_generic1

  • controller_generic2

Description — Create a MATLAB function that returns a cell array of models or subsystem file names. You can then specify the function name as a variant choices specifier.

For example, the getMyListOfControllerChoices function is available for you to use.

function choices = getMyListOfControllerChoices
        choices = {'linear_groupA_type1', 'linear_groupB_type2', ...
 'nonlinear_generic1', 'controller_generic2'};
end

ValuegetMyListOfControllerChoices

The model or the subsystem files returned by getMyListOfControllerChoices are added as variant choices. The added choices are:

  • linear_groupA_type1

  • linear_groupB_type2

  • nonlinear_generic1

  • controller_generic2

Description — Create a MATLAB function that returns a cell array of models or subsystem file names, and a file name pattern. You can then specify the function name as a variant choices specifier.

For example, getMyListOfControllers.

function choices = getMyListOfControllers
        choices = {'controller_generic1', 'ControllersList/nonlinear/**'};
end

ValuegetMyListOfControllers

The model and subsystem files returned by getMyListOfControllers are added as a variant choices. The added choices are:

  • nonlinear_groupA_type1

  • nonlinear_groupA_type2

  • nonlinear_groupB_type1

  • nonlinear_groupB_type2

  • nonlinear_generic1

  • nonlinear_generic2

  • controller_generic1

Description — Create a MATLAB function to exclude certain files or folders from the selection. You can then specify the function name as a variant choices specifier.

For example, the getMyFilteredListOfControllerChoices function is available for you to use.

function choices = getMyFilteredListOfControllerChoices
    % Pattern to include all the files inside 'ControllersList/'
    patternToInclude = 'ControllersList/**';

    % Pattern to exlude all the files inside 'ControllersList/linear/linear_groupA/'
    patternToExclude = 'ControllersList/linear/linear_groupA/*';
    
    % Set difference of files to include and exclude    
    choices = setdiff(getSLXFileNames(patternToInclude), getSLXFileNames(patternToExclude))';

    function fileNames = getSLXFileNames(pattern)
        [~, fileNames, fileExts] = fileparts({dir(pattern).name});
        fileNames = fileNames(strcmp(fileExts, '.slx'));
    end
end

ValuegetMyFilteredListOfControllerChoices

The model or subsystem files of ControllersList except those in ControllersList/linear/linear_groupA are added as variant choices. The added choices are:

  • linear_groupB_type1

  • inear_groupB_type2

  • linear_generic1

  • linear_generic2

  • nonlinear_groupA_type1

  • nonlinear_groupA_type2

  • nonlinear_groupB_type1

  • nonlinear_groupB_type2

  • nonlinear_generic1

  • nonlinear_generic2

  • controller_generic1

  • controller_generic2

Manipulate Variant Choices of Variant Assembly Subsystem Block in Expression Mode

This example shows how to add or remove variant choices from a Variant Assembly Subsystem block that is in the expression mode.

Maintain Variant Choices of Variant Assembly Subsystem Block in Model and Subsystem Files

Open the slexVariantAssemblySubsystemInExpression model. The model contains a Variant Assembly Subsystem block Controller. The block is in the expression mode and has two variant choices linearConroller and nonlinearController. The linearController and the nonlinearController blocks are Subsystem Reference blocks that are saved as subsystem files in your current working folder.

mdl = 'slexVariantAssemblySubsystemInExpression';
open_system(mdl)
vasBlkPath = [mdl, '/Controller'];

The linearController and nonlinearController blocks are added as choices to the Controller block using enumerations that are created using one of these methods. If you set the Variant activation time parameter of the Controller block to code compile or startup, Simulink sets the Treat as atomic unit parameter of the linearController and nonlinearController blocks to on. The Treat as atomic unit parameter enables Simulink to treat the blocks as a unit. For example, if the linearController block is set to atomic, Simulink simulates all the blocks within the linearController block before simulating the next block at the same level as the linearController.

  • Use a classdef block in a MATLAB™ file.

  • Use the function Simulink.defineIntEnumType. You do not need a script file to define the type.

  • Use a Simulink® data dictionary to permanently store the enumeration definition.

Define Enumeration Class Using classdef Block in MATLAB File

In this example, controllerTypesMfile is an integer-based enumeration class that is derived from the built-in class Simulink.IntEnumType. controllerTypesMfile is integer-based because IntEnumType is derived from int32. For more information on enumeration classes that are defined using the classdef block, see Create Simulink Enumeration Class.

type controllerTypesMfile
classdef controllerTypesMfile < Simulink.IntEnumType
%

%   Copyright 2023 The MathWorks, Inc.

  enumeration
    linearController(0)
    nonlinearController(1)
  end
end 


The enumeration section specifies the enumerated values linearController(0) and nonlinearController(1). Each enumerated value consists of an enumerated member and an underlying integer. Here, enumerated members are filenames of the variant choices linearController and nonlinearController and the underlying integers are 0 and 1.

1. To configure the Controller block, set the Variant control variable parameter to variantCtrlVar and the Variant choices enumeration parameter to controllerTypesMFile in the Block Parameters dialog box.

set_param(vasBlkPath, 'VariantChoicesEnumeration', 'controllerTypesMfile')
set_param(vasBlkPath, 'VariantControlVariable', 'variantCtrlVar')

2. To activate the linearController choice, set variantCtrlVar to controllerTypesMFile.linearController and then simulate the model. During simulation, the variantCtrlVar == controllerTypesMFile.linearController expression evaluates to true. Subsequently, the linearController choice becomes active.

variantCtrlVar = controllerTypesMfile.linearController;
sim(mdl);

Similarly, to activate the nonlinearController choice, set variantCtrlVar to controllerTypesMfile.nonlinearController and simulate the model again.

variantCtrlVar = controllerTypesMfile.nonlinearController;
sim(mdl);

Additionally, you can simulate both the variants and plot their values using this script.

in(1:2) = Simulink.SimulationInput(mdl);
in(1) = in(1).setVariable('variantCtrlVar', controllerTypesMfile.linearController);
in(2) = in(2).setVariable('variantCtrlVar', controllerTypesMfile.nonlinearController);
out = sim(in);
figure;
plot(out(1).tout, out(1).yout)
hold on
plot(out(2).tout, out(2).yout)
xlabel('Time (seconds)')
ylabel('Data')
[13-Feb-2024 00:04:57] Running simulations...
[13-Feb-2024 00:04:58] Completed 1 of 2 simulation runs
[13-Feb-2024 00:04:58] Completed 2 of 2 simulation runs

3. You can then add more variant choices to the Controller block by adding the variant choice filenames to the enumeration class. Similarly, you can remove choices from the Controller block by deleting the corresponding enumerated values from the enumeration class.

Define Dynamic Enumeration Class in Memory Using Simulink.defineIntEnumType

Create an enumeration class controllerTypesInMem in the MATLAB workspace using Simulink.defineIntEnumType.

Simulink.defineIntEnumType('controllerTypesInMem', ...
{'linearController', 'nonlinearController'}, 0:1);

The enumeration section specifies the enumerated values linearController(0) and nonlinearController(1). Each enumerated value consists of an enumerated member and an underlying integer. Here, enumerated members are filenames of the variant choices linearController and nonlinearController and the underlying integers are 0 and 1.

1. To configure the Controller block, set the Variant control variable parameter to variantCtrlVar and the Variant choices enumeration parameter to controllerTypesInMem in the Block Parameters dialog box.

set_param(vasBlkPath, 'VariantChoicesEnumeration', 'controllerTypesInMem')
set_param(vasBlkPath, 'VariantControlVariable', 'variantCtrlVar')

2. To activate the linearController choice, set variantCtrlVar to controllerTypesInMem.linearController and then simulate the model. During simulation, the variantCtrlVar == controllerTypesInMem.linearController expression evaluates to true. Subsequently, the linearController choice becomes active.

variantCtrlVar = controllerTypesInMem.linearController;
sim(mdl);

Similarly, to activate the nonlinearController choice, set variantCtrlVar to controllerTypesInMem.nonlinearController and simulate the model again.

variantCtrlVar = controllerTypesInMem.nonlinearController;
sim(mdl);

Additionally, you can simulate both the variants and plot their values using this script.

in(1:2) = Simulink.SimulationInput(mdl);
in(1) = in(1).setVariable('variantCtrlVar', controllerTypesInMem.linearController);
in(2) = in(2).setVariable('variantCtrlVar', controllerTypesInMem.nonlinearController);
out = sim(in);
plot(out(1).tout, out(1).yout)
hold on
plot(out(2).tout, out(2).yout)
xlabel('Time (seconds)')
ylabel('Data')
[13-Feb-2024 00:05:04] Running simulations...
[13-Feb-2024 00:05:04] Completed 1 of 2 simulation runs
[13-Feb-2024 00:05:04] Completed 2 of 2 simulation runs

3. To add more variant choices to the Controller block, redefine the controllerTypesInMem class with the new variant choice filenames as enumerated values. Similarly, to remove a choice, redefine the class with the corresponding enumerated value removed.

Permanently Store the Enumeration Definition in a Simulink Data Dictionary

Create an enumeration class controllerTypesInDD and a variant control variable variantCtrlVarInDD in the persistent repository data dictionary vas_controllersData.sldd. The enumeration section specifies the enumerated values linearController(0) and nonlinearController(1). Each enumerated value consists of an enumerated member and an underlying integer. Here, enumerated names are filenames of the variant choices linearController and nonlinearController and the underlying integers are 0 and 1. For more information on data dictionaries, see Permanently Store Enumerated Type Definition.

1. To configure the Controller block, set the Variant control variable parameter to variantCtrlVarInDD and the Variant choices enumeration parameter to controllerTypesInDD in the Block Parameters dialog box.

set_param(vasBlkPath, 'VariantChoicesEnumeration', 'controllerTypesInDD')
set_param(vasBlkPath, 'VariantControlVariable', 'variantCtrlVarInDD')

2. To activate the linearController choice, set variantCtrlVarInDD to controllerTypesInDD.linearController and then simulate the model. During simulation, the variantCtrlVarInDD == controllerTypesInDD.linearController expression evaluates to true. Subsequently, the linearController choice becomes active.

vasDDObj = Simulink.data.dictionary.open('vas_controllersData.sldd');
dDataSectObj = getSection(vasDDObj, 'Design Data');
evalin(dDataSectObj, 'variantCtrlVarInDD = controllerTypesInDD.linearController;');
out1 = sim(mdl)
out1 = 

  Simulink.SimulationOutput:
                   tout: [1001x1 double] 
                   yout: [1001x1 double] 

     SimulationMetadata: [1x1 Simulink.SimulationMetadata] 
           ErrorMessage: [0x0 char] 

Similarly, to activate the nonlinearController choice, set variantCtrlVar to controllerTypesInDD.nonlinearController and simulate the model again.

evalin(dDataSectObj, 'variantCtrlVarInDD = controllerTypesInDD.nonlinearController;');
out2 = sim(mdl)
out2 = 

  Simulink.SimulationOutput:
                   tout: [1001x1 double] 
                   yout: [1001x1 double] 

     SimulationMetadata: [1x1 Simulink.SimulationMetadata] 
           ErrorMessage: [0x0 char] 

Additionally, you can simulate both the variants and plot their values using this script.

figure;
plot(out1.tout, out1.yout)
hold on
plot(out2.tout, out2.yout)
xlabel('Time (seconds)')
ylabel('Data')

3. You can then add more variant choices to the Controller block by adding the variant choice filenames to the enumeration class. Similarly, you can remove choices from the Controller block by deleting the corresponding enumerated values from the enumeration class.

Generate Code for Variant Subsystems

For an example on generating code, see Generate Code for Variant Subsystem Blocks (Simulink Coder).

See Also

|