Main Content

金宝appSimulink.VariantVariable类

包裹:金宝app

创建一个变体参数对象

Description

Simulink.VariantVariableclass creates a variant parameter object. You can specify multiple values for each object. You can also specify properties such as dimension, data type, and so on. Each value of the object is associated with a variant condition expression. Set the parameter values and properties in the object, then reference the object from the block.

Before you create a newSimulink.VariantVariable目的,,,,create aSimulink.VariantControl目的representing the variant control variable to be used in theSimulink.VariantVariable目的。

笔记

You can create the variant parameter object only in a base or in a data dictionary.

Creation

p= Simulink.VariantVariablecreates an empty variant parameter object.

p= 金宝appsimulink.variantvariable(名称,价值创建一个或多个指定的变体参数对象名称,价值pair arguments. Using this syntax, you can specify multiple values for the object and associate each value with a variant condition expression. You can also specify other properties of the object such as data types, storage class, dimensions, and so on.

输入参数

expand all

名称值参数

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN,,,,whereName是参数名称和valueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:'Specification', 'Pspec', 'Choices', {'V==1', 4.5, 'V==2', 9.5}

properties of variant parameter object such as data type, dimensions, storage class, and so on, specified as the金宝appsimulink.parameter目的。这default storage class of a金宝appsimulink.parameter目的isAuto。如果将存储类指定为Auto,,,,the value of the object is inline to the literal value in the generated code. If you specify the storage class of the object anything other thanAuto,,,,the object is represented as tunable parameters using symbolic names in the generated code. For more information, seeOptions to Represent Variant Parameters in Generated Code(Embedded Coder)

Example:'Specification', 'Pspec'

变体条件和价值,,,,specified as a struct. During simulation, when a variant condition evaluates totrue,其关联的值变得活跃。当变体条件评估为错误的,其关联的值变得不活跃。相同变体参数的两个值必须与相同的变体条件相关联。

You can specify the variant condition as boolean MATLAB®包含一个或多个操作数和操作员的表达式。看变体控制变量(操作数)的类型在变体参数中and类型在变体参数中

您可以指定数字,枚举或金宝appsimulink.parameter类型。看,Numeric Variant Control Values for Rapid Prototyping of Variant Parameters,,,,Improve Code Readability of Variant Parameters Using Enumerated Types, 和Reuse Variant Parameter Values from Handwritten Code Using Simulink.Parameter Variables

Example:'Choices', {'V==1', 4.5, 'V==2', 9.5}

Methods

expand all

Examples

全部收缩

当您仍在构建模型时,数值允许您快速原型变体值。数值可以帮助您更多地专注于构建变体值,而不是开发激活这些选择的表达式。

You can define numeric control values in locations listed inStorage Locations for Variant Control Variables (Operands) in Variant Blocks

打开slexVariantParametersmodel.

open_system('slexVariantParameters'

在MATLAB编辑器中,选择t指定变体heir simplest form as numeric values inSimulink.VariantVariable对象K1andK2

k1 = 金宝appsimulink.variantvariable('Choices',,,,{'v == 1',3.5,'v == 2',,,,8.5})
K1= VariantVariable with 2 choices: Condition Value _________ _____ V == 1 3.5000 V == 2 8.5000 Specification: '' Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices
k2 = 金宝appsimulink.variantvariable('Choices',,,,{'v == 1',,,,4.5,,,,'v == 2',,,,9.5})
K2= VariantVariable with 2 choices: Condition Value _________ _____ V == 1 4.5000 V == 2 9.5000 Specification: '' Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices

Once you successfully create theSimulink.VariantVariable对象,,,,you can modify them by using the methods described inpublic Methodsor from thevariantVariabledialog box.

通过定义控制变量来激活一个变体值之一,,,,v, 和setting its value to1在一个Simulink.VariantControl目的。

v= Simulink.VariantControl('价值',,,,1,,,,'ActivationTime',,,,'update diagram'
v= VariantControl with properties: Value: 1 ActivationTime: 'update diagram'

When you simulate the model, the conditionV == 1评估trueK1is assigned a value of3.5andK2is assigned a value of8.5

sim('slexVariantParameters'

If you change the value ofvto2,s金宝appimulink®设定了K1andK2to4.5and9.5在模拟过程中。您可以更改价值v使用此命令或从Simulink.VariantControldialog box.

v。value = 2; sim ('slexVariantParameters'

如果您打算为包含变体参数的模型生成代码,请指定变体控制变量为金宝appsimulink.parameter对象。金宝appsimulink.parameter对象允许您指定其他属性,例如数据类型和存储类,并控制生成代码中变体控制变量的外观和放置。

  • You can define a variant control variable of type金宝appsimulink.parameteronly in the base workspace or in a data dictionary. Defining金宝appsimulink.parameter控制变量类型的变体在面具或model workspace is not supported. For more information on storage locations for variant control variables, seeStorage Locations for Variant Control Variables (Operands) in Variant Blocks

  • 金宝appsimulink.parameter对象within structures and that have data types other than金宝appsimulink.bus对象are not supported.

打开slexVariantParametersmodel.

open_system('slexVariantParameters'

In the MATLAB Editor, define a金宝appsimulink.parameter目的。

VSS_MODE= Simulink.Parameter; VSS_MODE.Value = 1; VSS_MODE.DataType ='int32';vss_mode.coderinfo.storageclass ='风俗';vss_mode.coderinfo.customstorageclass ='定义';vss_mode.coderinfo.customattributes.headerfile ='demo_macros.h';

变体控制变量定义为金宝appsimulink.parameter对象可以在Storage Classes for Different Variant Activation Times(Embedded Coder)

You can also convert a scalar variant control variable into a金宝appsimulink.parameter目的。For more information, seeConvert Variant Control Variables into Simulink.Parameter Objects

指定目的as a variant control inSimulink.VariantVariable对象K1andK2

k1 = 金宝appsimulink.variantvariable('Choices',,,,{'V == 1',3.5,'V == 2',,,,8.5})
K1= VariantVariable with 2 choices: Condition Value _________ _____ V == 1 3.5000 V == 2 8.5000 Specification: '' Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices
k2 = 金宝appsimulink.variantvariable('Choices',,,,{'V == 1',,,,4.5,,,,'V == 2',,,,9.5})
K2= VariantVariable with 2 choices: Condition Value _________ _____ V == 1 4.5000 V == 2 9.5000 Specification: '' Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices

Once you successfully create theSimulink.VariantVariable对象,,,,you can modify them by using the methods described inpublic Methodsor from thevariantVariabledialog box.

通过定义控制变量来激活一个变体值之一vand setting its value toVSS_MODE在一个Simulink.VariantControl目的。

v= Simulink.VariantControl('价值',vss_mode,'ActivationTime',,,,'code compile'
v= VariantControl with properties: Value: [1x1 Simulink.Parameter] ActivationTime: 'code compile'

When you simulate the model, the conditionV == 1评估trueK1is assigned a value of3.5, 和K2is assigned a value of8.5

sim('slexVariantParameters'

If you change the value ofVSS_MODEto2,s金宝appimulink®设定了K1andK2to4.5and9.5在模拟过程中。您可以更改价值v使用此命令或从Simulink.VariantControldialog box.

vss_mode.value = 2;v.value = vss_mode;sim('slexVariantParameters'

Generate code from the model. For information on how to generate code, seeGenerate Code Using Embedded Coder(Embedded Coder)

这GEnerated code contains both线性and非线性choices in preprocessor conditionals#如果and#elifbecause of thecode compileactivation time. The variant control variablevis defined using a macro —#定义directive — in the header filedemo_macros.h.You can control the appearance and placement ofvin the generated code and prevent optimizations from eliminating storage forvusing the storage class property. For more information, seeStorage Classes for Different Variant Activation Times(Embedded Coder)

%demo_macros.h%/* Exported data define */% / *自定义存储类的定义:定义 * /%#定义v2/* Referenced by:% *'/gain'%* '/Gain1'%*/%#endif /* RTW_HEADER_demo_macros_h_ */%/*

After identifying the variant values that your model requires, you can construct complex variant conditions to control the activation of your variant parameter values by defining variant conditions asSimulink.Variant对象。Simulink.Variant对象enable you to reuse common variant conditions across models and help you encapsulate complex variant condition expressions.

  • You can define a variant control variable of typeSimulink.Variantonly in the base workspace or in a data dictionary. DefiningSimulink.Variant控制变量类型的变体在面具或model workspace is not supported. For more information on storage locations for variant control variables, seeStorage Locations for Variant Control Variables (Operands) in Variant Blocks

  • Simulink.Variantwithin structures are not supported.

打开slexVariantParametersmodel.

open_system('slexVariantParameters'

In the MATLAB® Editor, encapsulate variant control expressions asSimulink.Variant对象。

线性Controller=Simulink.Variant('v == 1');NonLinearController=Simulink.Variant('v == 2');

指定Simulink.Variant对象作为变体控件Simulink.VariantVariable对象K1andK2

k1 = 金宝appsimulink.variantvariable('Choices',,,,{'LinearController',,,,3.5,,,,'NonLinearController',8.5})
K1= VariantVariable with 2 choices: Condition Value ___________________ _____ LinearController 3.5000 NonLinearController 8.5000 Specification: '' Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices
k2 = 金宝appsimulink.variantvariable('Choices',,,,{'LinearController',,,,4.5,,,,'NonLinearController',9.5})
k2 = variantvariable具有2个选择:条件值___________________ _____ linearController 4.5000 nonelarearcontroller 9.5000规格:''使用getchoice,setchoice,addchoice,addChoice,addChoice,removeChoice,removeChoice,removeChoice,removeChoice forcess,修改,添加或删除选择选择选择,

Once you successfully create theSimulink.VariantVariable对象,,,,you can modify them by using the methods described inpublic Methodsor from thevariantVariabledialog box.

Activate one of the variant values by defining aSimulink.VariantControl目的vand setting its value to1

v= Simulink.VariantControl('价值',,,,1,,,,'ActivationTime',,,,'update diagram'
v= VariantControl with properties: Value: 1 ActivationTime: 'update diagram'

When you simulate the model, the conditionV == 1评估trueK1is assigned a value of3.5,,,,andK2is assigned a value of8.5

sim('slexVariantParameters'

If you change the value ofvto2,s金宝appimulink®设定了K1andK2to4.5and9.5在模拟过程中。您可以更改价值v使用此命令或从Simulink.VariantControldialog box.

v。value = 2; sim ('slexVariantParameters'

Using this approach, you can develop complex variant condition expressions that are reusable.

Extended Capabilities

版本历史记录

在R2021a中引入