getVariable - Return value of variable in the model workspace of a model - MATLAB (original) (raw)
Main Content
Return value of variable in the model workspace of a model
Syntax
Description
[varValue](#d126e740783) = getVariable([mdlWks](#d126e740732),[varName](#d126e740754))
returns the value of the variable whose name is varName
that exists in the model workspace represented by theSimulink.ModelWorkspace
objectmdlWks
.
If the value of the target variable is a handle to a handle object (such asSimulink.Parameter
),getVariable
returns a copy of the handle. Changes you make to the variable in the model workspace or to the returned variable (variableValue
) affect both variables.
To return a deep copy of the handle object, use the copy
method of the object. To modify a handle object that you store in a model workspace, it is a best practice to use both the getVariable
andassignin
methods (see Modify Property Value of Handle Object).
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 variable named myVar
in the model workspace.
assignin(mdlWks,'myVar',5.12)
Return the value of the new variable. Store the value in another variable named varValue
.
varValue = getVariable(mdlWks,'myVar');
Modify a property of the Simulink.Parameter
object K
, which is defined in model mdl.slx
. When you callgetVariable
, use the copy
method because Simulink.Parameter
is a handle class.
wksp = get_param(mdl,'ModelWorkspace'); value = copy(getVariable(wksp,'K')); value.DataType = 'single'; assignin(wksp,'K',value);
Input Arguments
Target model workspace, specified as aSimulink.ModelWorkspace
object.
Name of the target variable, specified as a character vector.
Example: 'myVariable'
Data Types: char
Output Arguments
Value of the target variable, returned as a number, structure, or other MATLAB value.
Version History
Introduced in R2012a