Simulink.ModelWorkspace - Interact with the model workspace of a model programmatically - MATLAB (original) (raw)

Interact with the model workspace of a model programmatically

Description

Use a Simulink.ModelWorkspace object to interact with a model workspace. For example, you can add and remove variables, set the data source of the workspace, and save changes to the workspace.

For more information, see Model Workspaces.

Creation

To create a Simulink.ModelWorkspace, use the get_param function to query the value of the model parameterModelWorkspace. For example, to create an object namedmdlWks that represents the model workspace of a model namedmyModel.slx:

mdlWks = get_param('myModel','ModelWorkspace')

Properties

expand all

Source for initializing the variables in the model workspace, specified as one of these character vectors:

When DataSource is set to 'MATLAB Code' or 'MATLAB File', the MATLAB code cannot include any command that performs an update diagram, including:

Data Types: char

Name of the external file that stores or creates variables, specified as a character vector. To enable this property, setDataSource to 'MAT-File' or'MATLAB File'.

Example: 'myFile.mat'

Example: 'myFile.m'

Data Types: char

MATLAB code for initializing variables, specified as a character vector. To enable this property, set DataSource to'MATLAB Code'.

The MATLAB code cannot include any command that performs an update diagram, including:

Example: sprintf('%% Create variables that this model uses.\n\nK = 0.00983;\n\nP = Simulink.Parameter(5);')

Data Types: char

Object Functions

getVariable Return value of variable in the model workspace of a model
getVariablePart Get value of variable property in model workspace
setVariablePart Set property of variable in model workspace
hasVariable Determine whether variable exists in the model workspace of a model
whos Return list of variables in the model workspace of a model
saveToSource Save model workspace changes to the external data source of the model workspace
save Save contents of model workspace to a MAT-file
reload Reinitialize variables from the data source of a model workspace
evalin Evaluate expression in the model workspace of a model
clear Clear variables from the model workspace of a model
assignin Assign value to variable in the model workspace of a model

Examples

collapse all

Create a variable in the model workspace of a model. Then, modify the variable and query the variable value to confirm the modification.

Open the example model vdp.

openExample('simulink_general/VanDerPolOscillatorExample')

Create a Simulink.ModelWorkspace objectmdlWks that represents the model workspace ofvdp.

mdlWks = get_param('vdp','ModelWorkspace');

Create a variable named myVar with value5.12 in the model workspace.

assignin(mdlWks,'myVar',5.12)

Apply a new value, 7.22. To do so, first create a temporary copy of the variable by using the getVariable function. Then, modify the copy and use it to overwrite the original variable in the model workspace.

temp = getVariable(mdlWks,'myVar'); temp = 7.22; assignin(mdlWks,'myVar',temp)

Confirm the new value by querying the value of the variable.

getVariable(mdlWks,'myVar')

Version History

Introduced before R2006a

See Also