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.
To specify theGainparameters of theGain1andGain2blocks as variant parameters:
Define a
objectSimulink.VariantControl
V
in 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')
Create
objects 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,Simulink.VariantVariable
K1
andK2
are variant parameter objects ofSimulink.Parameter
type.K1
specifies multiple values for theGainparameter of theGain1block, andK2
specifies 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 the
Simulink.VariantVariable
objects, you can modify them by using the methods described inPublic Methodsor from the VariantVariable dialog box.Open the block parameter dialog box of theGainblocks. Specify theGainparameter ofGain1as
K1
and theGainparameter ofGain2asK2
.To simulate the model, on theSimulationtab, clickRun. During simulation, as
V==1
evaluates totrue
, the value of theGainparameter of theGain1block is set to3.5
, and the value of theGainparameter ofGain2block is set to8.5
.To change the value of theGainparameters, set the value of
V
to2
and run the simulation again. You can change the value ofV
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
as2
.In the MATLAB®Editor, execute this command:
V.Value = 2;
During simulation, as
V==2
evaluates totrue
, the value of theGainparameter of theGain1block is set to4.5
, and the value of theGainparameter of theGain2block is set to9.5
.
See Also
Use Variant Parameters to Reuse Block Parameters with Different Values|Options to Represent Variant Parameters in Generated Code(Embedded Coder)