Use Variant Parameters to Reuse Block Parameters with Different Values
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. Variant parameters can be defined in a MATLAB® base workspace or a data dictionary.
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 to
true
, 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 to
true
, 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 variable
V
. For example, if V==1
evaluates to
true
, then the value of fc
is set to
30
, es
is set to 1000
, and
nc
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 has preprocessor conditionals for each variant value of the parameter.
You can use Variant Manager to manage and activate variant parameters in your model.
Limitations of Using Variant Parameters
Using variant parameters in Model-Based Design also has limitations:
The value of the variant control variable that determines the active value of a variant parameter must be a numeric value, an enumerated value, or a
Simulink.Parameter
object.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 can only be defined in a base workspace or a data dictionary.
See Also
Simulink.VariantVariable
| Simulink.VariantControl
| Simulink.VariantBank
Related Topics
- Create a Simple Variant Parameter Model
- Change Active Values and Activation Time of Variant Parameters
- Options to Represent Variant Parameters in Generated Code (Embedded Coder)
- Compile Code Conditionally for All Values of Variant Parameters with Same and Different Dimensions
- Run Executable for Different Variant Parameter Values Without Recompiling Code