Reduce Model Containing Variant Parameters
This example shows how to reduce a Simulink® model that contains variant parameters using Variant Reducer in Variant Manager. The example explains the reduction process for these scenarios:
a. Reduction of these types of variant parameter objects for a single variant configuration:
With numeric values
With values of type
Simulink.Parameter
With the Specification property set
b. Reduction of variant parameters for multiple variant configurations
Variant parameters enable you to vary the values of block parameters in a Simulink model conditionally. You create a variant parameter as an object of type Simulink.VariantVariable
. This object defines the set of possible values for a parameter and a variant condition expression associated with each value. After creating the object, you can use it to set the value of block parameters in a model, such as the Gain parameter of a Gain block. During simulation, the value associated with the variant condition that evaluates to true
becomes the active value of that parameter. The values associated with the conditions that evaluate to false
become inactive. For more information, see Use Variant Parameters to Reuse Block Parameters with Different Values.
Using Variant Reducer, you can generate a reduced model that retains only the selected variant configurations that you choose from the parent variant model. The tool removes the model components that are inactive in the specified variant configurations and packages the reduced artifacts into a user-specified output folder. In the case of variant parameters, the reduction process retains only the specified values from the Simulink.VariantVariable
objects and removes the remaining inactive choices based on the input configurations that you specify.
For more information on Variant Reducer, see Reduce Variant Models Using Variant Reducer.
Explore Model
1. Open the model slexVariantParameters
.
open_system('slexVariantParameters');
The model contains two Gain
blocks. The Gain parameters of the blocks are set to the variant parameter objects K1
and K2
.
2. Open the slexVariantParameterData.m
file to see the definitions of objects used by this model.
K1
andK2
are defined as objects of typeSimulink.VariantVariable
. BothK1
andK2
have two choices. The choices have numericdouble
values and a variant condition expression associated with each value.
The variant condition is defined using a
Simulink.VariantControl
object,V
, with value set to1
. During simulation, the value ofV
determines the active value of the variant parameter objectsK1
andK2
.
The objects get created in the base workspace of the model during model load.
Double-click on
K1
andK2
in the MATLAB® workspace to view their possible values.
This example additionally refers to these type of variant parameters that are not present in the slexVariantParameters
model:
Variant parameters that have
Simulink.Parameter
objects as choice values
Variant parameters with an associated Specification, which helps you to set properties of the object such as data type, dimensions and storage class.
For example, this variant parameter has the values kp1
and kp2
, which are names of Simulink.Parameter
objects.
Open Variant Reducer
1. Open Variant Manager. On the Modeling tab, open the Design section and click Variant Manager.
2. To open Variant Reducer, in the Variant Manager toolstrip, in the Apps section, click Variant Reducer.
Choose Reduction Mode
The Reduction mode option in Variant Reducer provides multiple ways to specify the configurations that you want to retain in the reduced model. You can select single or multiple named variant configurations created for the model, or you can directly specify the required variant control variables and their values by defining variable groups.
When you reduce a variant parameter object for a single configuration, you specify only a single choice of the object that must be retained in the reduced model. The reduced object will have only one value without any variant capability. So, based on the type of value the variant parameter object has, the reduction process converts it into a normal MATLAB® variable with a numeric value or into a
Simulink.Parameter
object.
When you reduce a variant parameter object for multiple configurations, the reduction process retains the configurations that you specify in the reduced model. The process does not convert the
Simulink.VariantVariable
object to any other type.
Reduce Model for Single Variant Configuration
In the Variant Reducer window for the example model, the Variable Groups tab opens by default because there are no named configurations defined for the model. The Group table shows the current value of the variant control V
as 1
. In the example, choices of K1
and K2
have numeric double
values.
1. Specify the values for different reduction options as needed in the Options section of the toolstrip.
2. Click Reduce Model. Variant Reducer reduces the model for the condition V==1
.
3. Observe the objects K1
and K2
in the base workspace. You can see that K1
and K2
are converted to normal MATLAB variables of type double
, with values corresponding to the condition V==1
.
Instead, if the choice value of the variant parameter is a Simulink.Parameter
object, then after reduction the variant parameter object is converted into a Simulink.Parameter
object with values and properties based on the selected choice.
Variant Parameter Object with Specification
If the variant parameter object has the Specification property set, then these changes apply for single configuration reduction:
A variant parameter object with a numeric choice value is converted into a
Simulink.Parameter
object that has a value based on the selected configuration and remaining properties derived from the Specification property of the variant parameter object.
A variant parameter object with choice value set to a
Simulink.Parameter
object is not converted to any other type. It remains of typeSimulink.VariantVariable
and retains the selected configuration and Specification.
Reduce Model for Multiple Configurations
Close the reduced model and open the slexVariantParameters
model again.
1. In the base workspace, double-click the variant parameter object K1
. In the Simulink.VariantVariable
dialog box, add one more choice to K1
with Condition as V==3
and Value as 10.5
. Repeat this step for K2
.
2. Open Variant Reducer for the model.
3. In the Variable Groups tab, specify the value for V
as [1 3]
.
4. Click Reduce Model. Variant Reducer reduces the model for the conditions V==1
and V==3
.
5. Observe the objects K1
and K2
in the base workspace. You can see that they are variant parameter objects with two choices corresponding to V==1
and V==3
.
Reduce Model Programmatically
Reduce the model programmatically for the single configuration specified by the condition
V==1
.
Simulink.VariantManager.reduceModel('slexVariantParameters','VariableGroups',{{'V',1}});
Reduce the model for multiple configurations specified by the conditions
V==1
andV==3
.
Simulink.VariantManager.reduceModel('slexVariantParameters','VariableGroups',{{'V',1},{'V',3}});
Reduce the model by specifying a named variant configuration that is present in the variant configuration data object associated with the model.
Simulink.VariantManager.reduceModel('slexVariantParameters','NamedConfigurations',{'vpConfig1'},... 'OutputFolder','L:\reducedModel')
Summary of Variant Parameter Reduction
In a single configuration reduction, variant parameters convert as follows:
a. Without a Specification:
A variant parameter with numeric choice values converts to a normal MATLAB variable of same data type.
A variant parameter with
Simulink.Parameter
objects as choice values converts toSimulink.Parameter
object with values and properties based on the selected configuration.
b. With a Specification:
A variant parameter with numeric choice values converts to a
Simulink.Parameter
object that has values based on the selected configuration and remaining properties derived from the Specification property of the variant parameter object.
A variant parameter with
Simulink.Parameter
objects as choice values remains as a variant parameter object with the same Specification andSimulink.Parameter
objects as values, based on the selected configuration.
In a multiple configuration reduction, the model retains the variant parameter object with the selected configurations and Specification, if available.
NOTE: Specifying the value of a variant control variable as full-range
during reduction is not supported if the variable is used by variant parameters visible to the model, even if the parameter is not used in the model hierarchy.