Get value of variable from workspace


variableValue = getVariable(workspaceHandle,variableName)
variableValue = workspaceHandle.getVariable(variableName)


variableValue = getVariable(workspaceHandle,variableName) returns the value of the variable. If the variable does not exist in the workspace, an error occurs.

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 a 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).

variableValue = workspaceHandle.getVariable(variableName) is an alternative syntax.

Input Arguments


Handle to the workspace containing the variable.


Name of the variable containing the value.

Output Arguments


Value of the variable.


Get Value of Workspace Variable

Get the value of the workspace variable K, which is defined in model mdl.

wksp = get_param(mdl,'ModelWorkspace')
value = getVariable(wksp,'K')
value =

Modify Property Value of Handle Object

Modify a property of the Simulink.Parameter object K, which is defined in model mdl. 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';

See Also

Introduced in R2012a

Was this topic helpful?