Main Content

getVariable

Return value of variable in the model workspace of a model

Description

example

varValue = getVariable(mdlWks,varName) returns the value of the variable whose name is varName that exists in the model workspace represented by the Simulink.ModelWorkspace object mdlWks.

If the value of the target variable is a handle to a handle object (such as Simulink.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 and assignin methods (see Modify Property Value of Handle Object).

Examples

collapse all

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 call getVariable, 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

collapse all

Target model workspace, specified as a Simulink.ModelWorkspace object.

Name of the target variable, specified as a character vector.

Example: 'myVariable'

Data Types: char

Output Arguments

collapse all

Value of the target variable, returned as a number, structure, or other MATLAB value.

Version History

Introduced in R2012a