Main Content

Create a Simple Variant Parameter Model

这个例子向您展示了如何创建一个简单的模式l 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 totrue与赖斯有关,所有的值tion 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 with multiple values in a Gain block. Each value is associated with a variant condition expression.

To specify theGainparameters of theGain1andGain2blocks as variant parameters:

  1. Define aSimulink.VariantControlobjectVin the base workspace of the model. The variant control variable object determines the active value of the variant parameterGainof theGain1andGain2blocks. The object also associates the value with a variant activation time. For more information on activation time, seeActivate Variant During Different Stages of Simulation and Code Generation Workflow.

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

  2. CreateSimulink.VariantVariableobjects to define multiple values for theGainparameter of theGainblocks 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,K1andK2are variant parameter objects ofSimulink.Parametertype.K1specifies multiple values for theGainparameter of theGain1block, andK2specifies multiple values for theGainparameter of theGain2block. During simulation, when a condition evaluates totrue与赖斯有关,所有的值tion become active. The active values are then assigned to the correspondingGainparameter.

    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 theSimulink.VariantVariableobjects, you can modify them by using the methods described inPublic Methodsor from the VariantVariable dialog box.

    Create variant parameter object from VariantVariable dialog box

  3. Open the block parameter dialog box of theGainblocks. Specify theGainparameter ofGain1asK1and theGainparameter ofGain2asK2.

  4. To simulate the model, on theSimulationtab, clickRun. During simulation, asV==1evaluates totrue, the value of theGainparameter of theGain1block is set to3.5, and the value of theGainparameter ofGain2block is set to8.5.

  5. To change the value of theGainparameters, set the value ofVto2and run the simulation again. You can change the value ofVby performing any of these steps:

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

      Create variant control variable object from VariantControl dialog box

    • In the MATLAB®Editor, execute this command:

      V.Value = 2;

    During simulation, asV==2evaluates totrue, the value of theGainparameter of theGain1block is set to4.5, and the value of theGainparameter of theGain2block is set to9.5.

See Also

|(Embedded Coder)

Related Topics