Main Content

Simulink.ModelWorkspace

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 parameter ModelWorkspace. For example, to create an object named mdlWks that represents the model workspace of a model named myModel.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:

  • 'Model File' — The variables are stored in the model file. When you save the model, you also save the variables.

  • 'MATLAB Code' — The variables are created by MATLAB® code that you write and store in the model file.

  • 'MAT-File' — The variables are stored in a MAT-file, which you can manage and manipulate separately from the model file.

  • 'MATLAB File' — The variables are created by MATLAB code in a script file, which you can manage and manipulate separately from the model file.

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

  • sim

  • codegen

  • slbuild

  • set_param(model,'SimulationCommand','Update')

Data Types: char

Name of the external file that stores or creates variables, specified as a character vector. To enable this property, set DataSource 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:

  • sim

  • codegen

  • slbuild

  • set_param(model,'SimulationCommand','Update')

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

Data Types: char

Object Functions

getVariableReturn value of variable in the model workspace of a model
getVariablePartGet value of variable property in model workspace
setVariablePartSet property of variable in model workspace
hasVariableDetermine whether variable exists in the model workspace of a model
whosReturn list of variables in the model workspace of a model
saveToSourceSave model workspace changes to the external data source of the model workspace
saveSave contents of model workspace to a MAT-file
reloadReinitialize variables from the data source of a model workspace
evalinEvaluate expression in the model workspace of a model
clearClear variables from the model workspace of a model
assigninAssign 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 object mdlWks that represents the model workspace of vdp.

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

Create a variable named myVar with value 5.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')
ans =

    7.2200

Version History

Introduced before R2006a