Use Simulink.Parameter Objects as Values of
Simulink.VariantVariable Choices
Variant parameter objects enable you to conditionally vary the values of block parameters in a Simulink® model. For an overview of variant parameters, see Use Variant Control Variables in Variant Parameters.
You can set the values of choices of a variant parameter (Simulink.VariantVariable) to Simulink.Parameter objects. You can reuse the same
Simulink.Parameter object across multiple variant parameters in your model.
You can also specify code generation settings, such as storage class, for each choice of the
variant parameter. The following considerations apply when you use
Simulink.Parameter objects as values in a variant parameter:
The
Simulink.Parameterobjects must be defined in the same workspace as the variant parameter.All
Simulink.Parameterobjects used in a variant parameter must have the same data type, dimension, and complexity.When you assign
Simulink.Parameterobjects as the choices for a variant parameter and also set theSpecificationproperty of the variant parameter, Simulink verifies that properties such asDataTypeandDimensionsof theSimulink.Parameterchoices match those defined in the specification object.Simulink.Parameterobjects used to store a structure or an array of structures are supported if the data type of the structure is set to aSimulink.Busobject. For information on the use ofSimulink.Parameterobjects to store structures that contain block parameter definitions, see Control Field Data Types and Characteristics by Creating Parameter Object.Storage classes such as
DefineandImportedDefinethat generate macros in the code are not supported for theSimulink.VariantVariableobject.Storage classes such as
ConstandConstVolatilethat generate variables with aconsttype qualifier are not supported for theSimulink.VariantVariableobject.For
Simulink.VariantVariableobjects withAutostorage class, the code generator applies theModel defaultstorage class, resulting in a tunable variable in the generated code.Simulink.Parametervariables as values in a variant parameter must have storage classes listed in Storage Classes for Simulink.Parameter objects Used as Values of Variant Parameters (Simulink Coder).
Specify Simulink.Parameter Object as Variant Parameter Value
This example shows you how to specify the values of choices of variant parameters as Simulink.Parameter variables.
In a vehicle system model, consider the scenario where a parameter value needs to vary depending on whether the vehicle is configured as a sedan or a hatchback. To represent these two possible values, create two Simulink.Parameter objects kv_sedan and kv_hatchback. Use these objects to define the variant parameter.
1. Define the Simulink.Parameter objects.
kv_hatchback = Simulink.Parameter(3.5); kv_sedan = Simulink.Parameter(4.5); kv_hatchback.CoderInfo.StorageClass = "ImportedExtern"; kv_sedan.CoderInfo.StorageClass = "ImportedExtern";
2. Define a variant control variable.
V = Simulink.VariantControl(Value=VehicleType.Sedan,ActivationTime="startup");3. Define the variant parameter using the Simulink.Parameter objects. When you use this variant parameter in the model, the value of the variant parameter in simulation and code generation is determined from the values of the Simulink.Parameter objects.
kv = Simulink.VariantVariable(Choices={'V == VehicleType.Hatchback','kv_hatchback', ...
'V == VehicleType.Sedan','kv_sedan'});Here, using Simulink.Parameter variables with ImportedExtern storage class allows you to generate code that imports variant parameter values from your existing user-written code. Thus, you can reuse the values that your existing code defines. During code compilation, the generated code and the user-written code is integrated into a single executable. For an example, see Reuse Variant Parameter Values from Handwritten Code Using Simulink.Parameter Variables (Embedded Coder).
See Also
Simulink.VariantVariable | Simulink.VariantControl