Main Content

Configure Variant Parameter Values for Instances of Referenced Models

A model reference is a reference to another model using a Model block. You can reuse a model multiple times by referring to it with multiple Model blocks. These references create a model hierarchy. You can then configure a block parameter (such as the Gain parameter of a Gain block) to use either the same value or a different value for each instance of the model. To set different values for the same parameter across multiple model instances, create and use a model argument. For more information, see Configure Instance-Specific Values for Block Parameters in a Referenced Model.

Variant parameters enable you to conditionally vary the values of block parameters in a Simulink® model. Use the Simulink.VariantVariable class to create a variant parameter object. You create a variant parameter object in the base workspace, model workspace, or in the Design Data section of a Simulink data dictionary. For an example, see Create a Simple Variant Parameter Model.

Define Variant Parameters in Model Workspace for Use in Referenced Models

A model workspace effectively provides a model with its own namespace, allowing you to create variables for the model without risk of conflict with other models. When you define variant parameters in the model workspace, set the ActivationTime property of the Simulink.VariantControl object used as the variant control variable to update diagram, update diagram analyze all choices, or startup. The objects associated with the variant parameter object, such as the Simulink.VariantControl object, the Simulink.Parameter object used to set the Specification property, and any Simulink.Parameter objects used as values of the variant parameter, must be defined in the model workspace along with the parameter.

In a model reference hierarchy, you can define variant parameters with the same name but with different values scoped to the model workspaces of the referenced models.

In this example, the referenced models mModelWksRefModelA and mModelWksRefModelB each use variant parameters K and kv, and a variant control variable V defined in their respective model workspaces. In mModelWksRefModelA, V is set to 1 and in mModelWksRefModelB, V is set to 2. The top-level model mModelWksTopModel defines K and V in the base workspace with V set to 3.

mModelWksTopModel with a constant block that uses variant parameter K and two Model blocks that refer to mModelWksRefModelA and mModelWksRefModelB. Both mModelWksRefModelA and mModelWksRefModelB have the same model hierarchy with two variant parameters K and kv.

This image shows the results of activating the workspace variant configuration for the model mModelWksTopModel from Variant Manager. The active choice for the variant parameter in each model depends on the value of V in the corresponding workspace. For information on managing variant parameters, see Manage Variant Parameters Using Variant Manager.

Variant Parameters tab in Variant Manager shows the results of activating the variant parameters. The active choice is highlighted for the parameters in each of the workspaces.

Specify Instance-Specific Values for Variant Parameters in a Referenced Model

To configure a variant parameter in the referenced model to use a different value for each instance of the model, configure the Simulink.VariantControl object defined in the model workspace of the referenced model as a model argument by selecting the Argument property in the Model Explorer. In each Model block that refers to this model, specify an instance-specific value for the variant control from the Block Parameters dialog box.

Open the model slexVariantParameterMultiInstance and the referenced model slexVariantParameterWiper.

open_system("slexVariantParameterMultiInstance");
open_system("slexVariantParameterWiper");

  • The top-level model slexVariantParameterMultiInstance uses the referenced model slexVariantParameterWiper to implement a system that takes the speed of a car wiper motor as the input and computes the sweep time and sweep area parameters of the wiper. The model is configured to automatically switch between two input sources for simulation and code generation workflows: a Step block for simulation and the driver-provided wiper setting from the car for code generation. To achieve this, the model uses two Variant Source blocks with the Variant control mode parameter set to sim codegen switching. The step signal is connected to the sim input of the Variant Source block, and the driver-provided setting is connected to the codegen input.

  • The top-level model computes the SweepTime and SweepArea parameters for the front driver, front passenger, and rear wipers of the car by providing the corresponding wiper motor speed as input to three instances of the slexVariantParameterWiper model.

  • The slexVariantParameterWiper model takes the motor speed as input and uses three variant parameters GearRatio, SweepAngle, and WiperLength defined in the model workspace to calculate the value of the required output parameters. These variant parameters take a different value for each of the driver, passenger, and rear wiper positions. The variant parameters use a Simulink.VariantControl object named Wiper to specify the variant conditions for the choices. The Wiper value can be set to WiperPosition.FRONT_DRIVER, WiperPosition.FRONT_PASSENGER, or WiperPosition.REAR defined in the WiperPosition.m file. The ActivationTime property of Wiper is set to startup and is specified as a model argument.

  • In the top-level model, for each instance of the slexVariantParameterWiper model, the model argument Wiper is set to an instance-specific value corresponding to the wiper position. During simulation, the active choice of variant parameters in the referenced model vary for each instance based on the value of Wiper set from the top-level model. This image shows the instance value specified for Wiper in the Block Parameters dialog box of the Model block Front Driver Wiper.

Simulate the model. Open the Simulation Data Inspector and compare the logged signals to see how the output varies for different instances of the same referenced model.

See Also

|

Topics