Reduce Model Containing Variant Blocks
This example shows how to reduce a Simulink® model that contains variant blocks both programmatically and by using Variant Reducer in Variant Manager.
A variant model can have multiple variant configurations associated with it. Each variant configuration represents a combination of variant control values that activates a specific variant choice. You can generate a reduced version of a variant model that contains only a subset of variant configurations from the original model using Variant Reducer.
Explore Model
Open the model slexVariantReducer
. The model contains a Variant Source block, a Variant Sink block, and a Variant Subsystem block with these variant choices:
Variant Source:
V == 1
andV == 2
Variant Sink:
W == 1
,W == 2
, andW == 3
Variant Subsystem:
V == 1
andV == 2
On the Modeling tab of the Simulink toolstrip, select Model Explorer > Model Properties > Callbacks. The PostLoadfn
callback function of the model defines the variant control variables V
and W
and loads the slexVariantReducer_config.mat
file.
The model has two predefined variant configurations named config1
(V == 1 && W == 2
) and config2
(V == 2 && W == 2
). These configurations are saved in a variant configuration data object, slexVariantReducer_config
, which is in turn saved in the slexVariantReducer_config.mat
file.
open_system('slexVariantReducer');
Reduce Model from Variant Manager
Open Variant Manager
1. To open Variant Manager, right-click the variant badge on any of the variant blocks and select Open in Variant Manager. The Variant Manager window opens and shows the predefined variant configurations.
2. To open Variant Reducer, in the Variant Manager toolstrip, in the Apps section, click Variant Reducer. The Variant Reducer window opens.
Choose Reduction Mode
Using the Reduction mode option in Variant Reducer, you can specify the configurations that you want to retain in the reduced model.
Current values of variant control variables in the base workspace or data dictionary used by the model
Named variant configurations created for the model
Variable groups that contain variant control variables and their values
Select either a named configuration or a variable group as detailed in the following sections.
Specify Named Variant Configuration
1. In the Variant Reducer toolstrip, from the Reduction mode list, select the Specify variant configurations option.
2. Select the configuration to be retained in the reduced model from the Configurations tab.
Specify Named Variable Group
1. In the Variant Reducer toolstrip, from the Reduction mode list, select the Specify variant control values option.
2. Select the group of variables to be retained in the reduced model from the Variable Groups tab. To create a new named variable group, click the Add variable group button.
3. In the Group table, in the Values column, you can either specify a value for the variant control variable or select Full-range
or Ignored
from the list.
Reduce
1. You can modify the other reduction settings in the Mode and Options section of the Variant Reducer toolstrip as needed. If you do not modify the settings, Variant Reducer uses default values for reduction.
2. Click Reduce Model. The reduced model for the selected configurations is created.
If the model contains resolved library links or referenced models, the corresponding parent is reduced for the specified configuration and is referenced in the model. The reduced model, reduced referenced model, and reduced library get their names from the corresponding model, referenced model, or library with _r
(model suffix) appended to it.
Reduce Model Programmatically
To reduce the model programmatically, run either of these commands in the MATLAB® Command Window:
Generate a reduced model by specifying a variable group and place reduced artifacts in the specified output folder.
Simulink.VariantManager.reduceModel('slexVariantReducer', 'VariableGroups',{'V', 1,'W', 1}, 'OutputFolder', 'L:/reducedmodel');
Generate a reduced model for the named variant configuration
config1
to the default output folderreducedModel
and open the reduced model.
Simulink.VariantManager.reduceModel... ('slexVariantReducer', 'NamedConfigurations', {'config1'}); cd ./reducedModel; open_system('slexVariantReducer_r');
Successfully created reduced model '/tmp/Bdoc24b_2679053_907440/tpe5f5767d/simulink_variants-ex21847392/reducedModel/slexVariantReducer_r.slx'.
You can specify other options when reducing a model programmatically; see Simulink.VariantManager.reduceModel
.