主要内容

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.Parameter objects must be defined in the same workspace as the variant parameter.

  • All Simulink.Parameter objects used in a variant parameter must have the same data type, dimension, and complexity.

  • When you assign Simulink.Parameter objects as the choices for a variant parameter and also set the Specification property of the variant parameter, Simulink verifies that properties such as DataType and Dimensions of the Simulink.Parameter choices match those defined in the specification object.

  • Simulink.Parameter objects used to store a structure or an array of structures are supported if the data type of the structure is set to a Simulink.Bus object. For information on the use of Simulink.Parameter objects to store structures that contain block parameter definitions, see Control Field Data Types and Characteristics by Creating Parameter Object.

  • Storage classes such as Define and ImportedDefine that generate macros in the code are not supported for the Simulink.VariantVariable object.

  • Storage classes such as Const and ConstVolatile that generate variables with a const type qualifier are not supported for the Simulink.VariantVariable object.

  • For Simulink.VariantVariable objects with Auto storage class, the code generator applies the Model default storage class, resulting in a tunable variable in the generated code.

  • Simulink.Parameter variables 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

|

Topics