Main Content

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

This example shows how to add or remove the variant choices of a Variant Assembly Subsystem block without modifying the model. You can use external sources to manage the variant choices of the Variant Assembly Subsystem. For more information, see Variant Assembly Subsystem.

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

A Variant Assembly Subsystem block can contain Model blocks, Subsystem Reference 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 slexVariantAssemblySubsystem 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.

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 Validate button. On a successful validation, the blocks are added as variant choices to the Variant Assembly Subsystem block. The file names of the newly added variant choices and their corresponding absolute paths 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 Validate 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

See Also

|