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 themodel1.slx
andsubsystemRef1.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 vectorsfolder_1/*
andfolder2/*
to thedir
function. The function then returns the model and the subsystem files that are located inside thefolder1
andfolder2
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 Specifier | Added Variant Choices |
---|---|
Description — Specify a relative path to a folder that contains model or subsystem files. Value —
| The model or subsystem files that are located inside
the
|
Description — Specify relative path to a folder and its subfolders that contain model or subsystem files. Value —
| The model or subsystem files that are located inside
the
|
Description — Specify relative path to a folder and its subfolders one level down that contain model or subsystem files. Value —
| The model or subsystem files that are located inside
the
|
Description — Specify relative path to a folder that contain model or subsystem files and has subfolder names with a certain ending. Value —
| The model or subsystem files that are located inside
the
|
Description — Specify an absolute path to a folder that contains model or subsystem files Value — In Windows® systems,
In
UNIX® systems,
| The model or subsystem files that are located inside
the
|
Description — Specify the model or subsystem file names in a cell array Value
— | The specified model and subsystem files are added as variant choices. The added choices are:
|
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 = {'linear_groupA_type1', 'linear_groupB_type2', ... 'nonlinear_generic1', 'controller_generic2'}; Value —
| The model or subsystem files specified in
|
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
function choices = getMyListOfControllerChoices choices = {'linear_groupA_type1', 'linear_groupB_type2', ... 'nonlinear_generic1', 'controller_generic2'}; end Value —
| The model or the subsystem files returned by
|
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,
function choices = getMyListOfControllers choices = {'controller_generic1', 'ControllersList/nonlinear/**'}; end Value —
| The model and subsystem files returned by
|
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
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 Value —
| The model or subsystem files of
|
See Also
Control Variant Choices in Masked Variant Assembly Subsystem Block Using Mask Parameter Object | Convert Variant Subsystem to Variant Assembly Subsystem