Main Content

Create a Simple Variant Parameter Model

This example shows you how to create simple model with variant parameters.

Variant parameters provide you a way to design one model for many systems that differ in 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, all the values associated with that condition become active. Use variant parameters to switch among different block parameter values for multiple simulation, code generation, or testing workflows.

Consider this model.

Variant parameter in a Gain block

To specify the Gain parameters of the Gain1 and Gain2 blocks as variant parameters:

  1. Define a Simulink.VariantControl object V in the base workspace of the model. The variant control variable object determines the active value of the variant parameter Gain of the Gain1 and Gain2 blocks. The object also associates the value with a Variant activation time.

    V = Simulink.VariantControl('Value', 1, 'ActivationTime', 'update diagram')
    

  2. Create Simulink.VariantVariable objects to define multiple values for the Gain parameter of the Gain blocks and associate each value with a variant condition expression. You can specify properties such as dimension, data type, and so on for each object. In this example, K1 and K2 are variant parameter objects of Simulink.Parameter type. K1 specifies multiple values for the Gain parameter of the Gain1 block, and K2 specifies multiple values for the Gain parameter of the Gain2 block. During simulation, when a condition evaluates to true, all the values associated with that condition become active. The active values are then assigned to the corresponding Gain parameter.

    Pspec = Simulink.Parameter(0)
    Pspec.CoderInfo.StorageClass = 'ExportedGlobal'
    K1 = Simulink.VariantVariable('Specification', 'Pspec', 'Choices', {'V==1', 3.5, 'V==2', 8.5})
    K2 = Simulink.VariantVariable('Specification', 'Pspec', 'Choices', {'V==1', 4.5, 'V==2', 9.5})
    

    Once you successfully create the Simulink.VariantVariable objects, you can modify them by using the methods described in Public Methods or from the VariantVariable dialog box.

    Variant parameter object from VariantVariable dialog box

  3. Open the block parameter dialog box of the Gain blocks. Specify the Gain parameter of Gain1 as K1 and the Gain parameter of Gain2 as K2.

  4. To simulate the model, on the Simulation tab, click Run. During simulation, as V==1 evaluates to true, the value of the Gain parameter of the Gain1 block is set to 3.5, and the value of the Gain parameter of Gain2 block is set to 8.5.

  5. To change the value of the Gain parameters, set the value of V to 2 and run the simulation again. You can change the value of V by performing any of these steps:

    • Double-click the variant control variable created in the base workspace. In the VariantControl dialog box, specify the value of V as 2.

      Variant control variable object from VariantControl dialog box

    • In the MATLAB® Editor, execute this command:

      V.Value = 2;

    During simulation, as V==2 evaluates to true, the value of the Gain parameter of the Gain1 block is set to 4.5, and the value of the Gain parameter of the Gain2 block is set to 9.5.

See Also

| (Embedded Coder)

Related Topics