Use Variant Parameters to Reuse Block Parameters with Different Values - MATLAB & Simulink (original) (raw)
Main Content
You may have a set of requirements where the structure of the model remains the same, but the values of the parameters for each requirement are different. In this scenario, you can create a single model with fixed structure and create parameters with a finite set of values where each value corresponds to different requirements. Parameters that have multiple values are called variant parameters. Use the Simulink.VariantVariable class to create a variant parameter object.
Each value of a variant parameter must be associated with a variant condition expression. You can have multiple variant parameters associated with a variant condition, provided no two values belong to the same parameter. When a condition expression evaluates totrue
, all the values associated with that condition become active and are assigned to the corresponding parameters. If you change the value of the variant control variable, a different set of values is assigned to the parameters. This way, variant parameters allow you to logically bind a set of values across different parameters to a condition expression, and the values are assigned to the parameter based on the variant condition that evaluates to true
.
SimulinkĀ® selects the active value of a variant parameter during update diagram time, during code compile time, or during the model startup time. For more information, see Activate Variant During Different Stages of Simulation and Code Generation Workflow.
Consider this model. The Gain parameter of the Gain block is a variant parameter with its value set to K. The variable_K_ has two values: 3.5
and 8.5
. Simulink chooses active value of K based on the value of the variant control variable V. If V==1
evaluates totrue
, the value of K is set to 3.5
. If V==2
evaluates to true
, the value of_K_ is set to 8.5
.
When to Create Variant Parameters
Variant parameters provide a way to design one model for many systems that differ in block parameter values. You can group different sets of values based on the system to simulate. Each set of values is associated with a variant condition expression. During simulation, if a variant condition evaluates to true
, then all the values associated with that condition become active. To change the set of active values, change the value of the variant control variable and run the simulation again.
Consider this model that represents an automobile system with several configurations. These configurations, although similar in several aspects, can differ in values such as fuel consumption, engine size, or number of cylinders in the engine. Instead of designing multiple blocks that together represent all possible values, you can use variant parameters to model the varying values.
In this model, the Fuel consumption, Engine size, and Number of cylinders have variant parameters fc
, es
, and nc
. The values of the variant parameters are based on the value of the variant control variableV
. For example, if V==1
evaluates totrue
, then the value of fc
is set to30
, es
is set to 1000
, andnc
is set to 4
.
Advantages of Using Variant Parameters
Using variant parameters in Model-Based Design provides several advantages:
- Variant parameters provide you a way to design one model for many systems.
- Variant parameters facilitate reuse of blocks. This approach improves workflow speed by reducing complexity.
- Variant parameters help you switch among different block parameter values for multiple simulation, code generation, or testing workflows.
- You are not required to regenerate the code for different values of a variant parameter because the generated code includes all variant values of the parameter when the variant activation time is set to
code compile
orstartup
. - You can use Variant Manager to manage and activate variant parameters in your model.
Limitations
- Variant parameters can only be defined in a base workspace, model workspace, or a data dictionary.
- Only the values of the variant parameters change based on the variant condition that evaluates to
true
. Other properties, such as storage class, data types, and so forth, remain the same irrespective of the variant condition. - Variant parameters that are part of a variant parameter bank do not support AUTOSAR code generation.
- The Value property of the variant control variable (Simulink.VariantControl), which determines the active value of a variant parameter, must be one of these types:
- Integer
- Enumerated value
Simulink.Parameter
object with value of type integer, enumeration, or a mathematical expression specified using theslexpr
function.- User-defined type that inherits from
Simulink.Parameter
- Using the
slexpr
function or aSimulink.data.Expression
object directly to specify a mathematical expression for the value of a choice of a variant parameter or the variant control used by the variant parameter is not supported. Instead, define the value of the variant parameter or the variant control by using aSimulink.Parameter
object with its value set to the mathematical expression by using theslexpr
function. - Using a Parameter Writer block to modify the value of the
Simulink.VariantControl
object used in aSimulink.VariantVariable
object is not supported in these scenarios:- The
Simulink.VariantControl
value is modified in multipleInitialize Function blocks. - The condition expression of a
Simulink.VariantVariable
object contains multipleSimulink.VariantControl
objects. - The
Simulink.VariantVariable
object is part of a variant parameter bank (Simulink.VariantBank
). - The Parameter Writer block is connected to a block that uses a
Simulink.VariantVariable
object. In this case, theParameter Writer block supports only changing block parameter values. - The
Simulink.VariantControl
object is configured as a model argument. - The model is simulated in accelerator or rapid accelerator mode when the
Simulink.VariantControl
object is defined in the model workspace. - The Parameter Writer block is used to modify the value of a
Simulink.Parameter
object that is used as the value of aSimulink.VariantVariable
choice. - The
Simulink.VariantVariable
object is used in an expression that contains multiple variables, for example,K + A
whereK
is aSimulink.VariantVariable
object andA
is a MATLABĀ® variable.
- The
See Also
Simulink.VariantVariable | Simulink.VariantControl | Simulink.VariantBank