Simulink.VariantVariable class
Package:金宝app
Create a variant parameter object
Description
TheSimulink.VariantVariable
class 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
object, create aSimulink.VariantControl
object representing the variant control variable to be used in theSimulink.VariantVariable
object.
Note
You can create the variant parameter object only in a base or in a data dictionary.
Creation
creates an empty variant parameter object.P
= Simulink.VariantVariable
creates a variant parameter object as specified by one or moreP
= Simulink.VariantVariable(Name,Value
)Name,Value
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.
Input Arguments
Specify optional comma-separated pairs ofName,Value
arguments.Name
is the argument name andValue
is the corresponding value.Name
must appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN
.
'Specification', 'Pspec', 'Choices', {'V==1', 4.5, 'V==2', 9.5}
Specification
—Properties of variant parameter object
Simulink.Parameter
object name
Properties of variant parameter object such as data type, dimensions, storage class, and so on, specified as theSimulink.Parameter
object. The default storage class of aSimulink.Parameter
object isAuto
. If you specify the storage class asAuto
, 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
对象表示为可调参数rs using symbolic names in the generated code. For more information, seeChoose Storage Class for Controlling Data Representation in Generated Code(Simulink Coder).
Note
If the variant parameter object is associated to a variant control object having astartup
activation time then specifying the storage class of the variant parameter object to any of these is not supported:
Storage classes such as
Define
that generates macros in the code.Storage classes such as
Const
that generates constant qualified variables in the code.
Example:'Specification', 'Pspec'
Choices
—Variant conditions and values
cell array
Variant conditions and values, specified as a struct. During simulation, when a variant condition evaluates totrue
, its associated value becomes active. When a variant condition evaluates tofalse
, its associated value becomes inactive. No two values of the same variant parameter must be associated with the same variant condition.
You can specify the variant condition as boolean MATLAB®expressions that contain one or more operands and operators. SeeVariant Controls in Variant Parameters.
Example:'Choices', {'V==1', 4.5, 'V==2', 9.5}
Methods
Public Methods
addChoice |
Add choices to variant parameter object |
getChoice |
Return choices of variant parameter object |
setChoice |
Modify existing choices of variant parameter object |
removeChoice |
Remove choices from variant parameter object |
Specialized Operators and Functions
These methods specialize standard MATLAB operators and functions for objects in this class.
bitcmp |
Performs bit-wise complement on input. |
情商 |
Compares if the inputs are equal. |
gt |
Compares if the first input is greater than the second input. |
mrdivide |
Divides the first input by the second input. |
or |
Performs a logical OR on inputs. |
uminus |
Negates the input. |
bitor |
Performs bit-wise OR on inputs. |
ge |
Compares if the first input is greater than or equal to the second input. |
le |
Compares if the first input is less than or equal to the second input. |
mtimes |
Multiplies the inputs. |
plus |
Adds the inputs. |
uplus |
Returns the value of the input. |
and |
Performs logical AND on inputs. |
bitshift |
Shift bits by specified number of places. |
lt |
Compares if the first input is less than the second input. |
ne |
Compares if the inputs are not equal. |
bitand |
Performs bit-wise AND on inputs. |
bitxor |
Performs bit-wise XOR on inputs. |
minus |
Subtracts the second input from the first input. |
not |
Returns logical NOT of input. |
Examples
Scalar Values for Rapid Prototyping of Variant Parameters
Scalar MATLAB® variables allow you to rapidly prototype variant values when you are still building your model. They help you focus more on building your variant values than on developing the expressions that activate those choices.
You can define scalar control variables in locations listed inStorage Locations for Variant Control Variables (Operands) in Variant Condition Expressions.
In the MATLAB Editor, specify variant choices in their simplest form as scalar variables in aSimulink.VariantVariable
object.
vpObj = Simulink.VariantVariable('Choices',{'VCtrl==1', 1000,'VCtrl==2', 2000})
vpObj = VariantVariable with 2 choices: Condition Value __________ _____ VCtrl == 1 1000 VCtrl == 2 2000 Specification: '' Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices
Activate one of the variant values by defining a control variable,VCtrl
, and setting its value to1
in aSimulink.VariantControl
object. This condition activates the value1000
and assigns this value tovpObj
.
VCtrl = Simulink.VariantControl('Value', 1,'ActivationTime','update diagram')
VCtrl = VariantControl with properties: Value: 1 ActivationTime: 'update diagram'
If you change the value ofVCtrl
to2
, Simulink® sets the value ofvpObj
to2000
.
VCtrl.Value = 2;
Simulink.Parameter Objects for Code Generation of Variant Parameters
If you intend to generate code for a model containing variant parameter values, specify variant control variables as MATLAB® variables orSimulink.Parameter
objects.Simulink.Parameter
objects allow you to specify other attributes (such as data type) that are required for generating code.
Note:
You can define a variant control variable of type
Simulink.Parameter
only in the base workspace or in a data dictionary. DefiningSimulink.Parameter
type of variant control variables in the mask or model workspace is not supported. For more information on storage locations for variant control variables, seeStorage Locations for Variant Control Variables (Operands) in Variant Condition Expressions.Simulink.Parameter
objects within structures and that have data types other thanSimulink.Bus
objects are not supported.
In the MATLAB Editor, define aSimulink.Parameter
object.
VSSMODE = Simulink.Parameter; VSSMODE.Value = 1; VSSMODE.DataType ='int32'; VSSMODE.CoderInfo.StorageClass ='Custom'; VSSMODE.CoderInfo.CustomStorageClass ='ImportedDefine'; VSSMODE.CoderInfo.CustomAttributes.HeaderFile ='rtwdemo_importedmacros.h';
Variant control variables defined asSimulink.Parameter
objects can have one of these storage classes:
Define or ImportedDefine
with header file specifiedCompilerFlag
SystemConstant
(AUTOSAR)Your own storage class that defines data as a macro
You can also convert a scalar variant control variable into aSimulink.Parameter
object. For more information, seeConvert Variant Control Variables into Simulink.Parameter Objects.
Specify the object as a variant control using aSimulink.VariantVariable
object.
vpObj = Simulink.VariantVariable('Choices', {'VCtrl == 1',1000,'VCtrl == 2',2000});
Activate one of the variant values by defining a control variableVCtrl
and setting its value toVSSMODE
in aSimulink.VariantControl
object. This condition activates the value1000
and assigns this value tovpObj
.
VCtrl = Simulink.VariantControl('Value',VSSMODE,'ActivationTime','code compile')
VCtrl = VariantControl with properties: Value: [1x1 Simulink.Parameter] ActivationTime: 'code compile'
Simulink.Variant Objects for Variant Condition Reuse of Variant Parameters
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
objects.Simulink.Variant
objects enable you to reuse common variant conditions across models and help you encapsulate complex variant condition expressions.
Note:
You can define a variant control variable of type
Simulink.Variant
only in the base workspace or in a data dictionary. DefiningSimulink.Variant
type of variant control variables in the mask or model workspace is not supported. For more information on storage locations for variant control variables, seeStorage Locations for Variant Control Variables (Operands) in Variant Condition Expressions.Simulink.Variant
within structures are not supported.
In the MATLAB® Editor, encapsulate variant control expressions asSimulink.Variant
objects.
LinearController=Simulink.Variant('VCtrl==1'); NonLinearController=Simulink.Variant('VCtrl==2');
Specify theSimulink.Variant
objects as the variant controls in aSimulink.VariantVariable
object.
vpObj = Simulink.VariantVariable('Choices',{'LinearController',1000,'NonLinearController',2000})
vpObj = VariantVariable with 2 choices: Condition Value ___________________ _____ LinearController 1000 NonLinearController 2000 Specification: '' Use getChoice, setChoice, addChoice, removeChoice to access, modify, add or remove choices
Activate one of the variant values by defining a control variable,VCtrl
, and setting its value toLinearController
in aSimulink.VariantControl
object. This condition activates the value1000
and assigns this value tovpObj
.
VCtrl = Simulink.VariantControl('Value',1,'ActivationTime','update diagram')
VCtrl = VariantControl with properties: Value: 1 ActivationTime: 'update diagram'
Using this approach, you can develop complex variant condition expressions that are reusable.
See Also
Apri esempio
如果dispone di una versione modificata di questo esempio. Desideri aprire questo esempio con le tue modifiche?
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)