setVariablePart - Set property of variable in model workspace - MATLAB (original) (raw)
Set property of variable in model workspace
Syntax
Description
[varValue](#mw%5F15abb0c4-37a3-4823-b065-bc6ab0f18074) = setVariablePart([mdlWks](#mw%5Fef1a4087-477d-4ba3-ae9e-623e41835b86),[varName.Property](#mw%5Fc3c93e65-6828-40d7-845d-12c6ddb26ced), [varValue](#mw%5F15abb0c4-37a3-4823-b065-bc6ab0f18074))
assigns varValue
to the MATLAB variable propertyvarName.Property
in the model workspace represented by theSimulink.ModelWorkspace
object mdlWks
.
Using setVariablePart
is preferable to using evalin for assigning variable properties in the model workspace because thesetVariablePart
function does not create a new variable or cause unintended results.
Examples
Open the example model vdp
.
openExample('simulink_general/VanDerPolOscillatorExample')
Create a Simulink.ModelWorkspace
object that represents the model workspace of vdp
.
mdlWks = get_param('vdp','ModelWorkspace');
Create a structure named myStruct
with fieldsa
, b
, and c
.
aStruct.a = 10; aStruct.b = {1,2,3,4,5}; aStruct.c = Simulink.Parameter(7); mdlWks.assignin('myStruct',aStruct);
Assign new values to the structure fields.
setVariablePart(mdlWks,'myStruct.a', 2); setVariablePart(mdlWks,'myStruct.b{1}', 2); setVariablePart(mdlWks,'myStruct.c', Simulink.Parameter(2));
Return the new values of the structure fields. Store the values invarValuea
, varValueb
, andvarValuec
.
varValuea = getVariablePart(mdlWks,'myStruct.a') varValueb = getVariablePart(mdlWks,'myStruct.b{1}') varValueC = getVariablePart(mdlWks,'myStruct.c.Value')
Input Arguments
Target model workspace, specified as a Simulink.ModelWorkspace
object.
Name of the target variable property, specified as a character vector.
Example: 'myVariable.Property'
Data Types: char
| string
Value to assign to the value property, specified as a valid value. For example, you can specify a literal number, a structure, or an expression that evaluates to a valid value.
If you specify the name of a handle object, such as aSimulink.Parameter
object, use the copy
function to create a separate copy of the object.
Example: 5.12
Example: struct('a',5.12,'b',7.22)
Example: Simulink.Parameter(5.12)
Example: copy(myExistingParameterObject)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| table
| cell
| categorical
| datetime
| duration
| calendarDuration
| fi
Complex Number Support: Yes
Version History
Introduced in R2018b