Skip to Main Content Skip to Search
Product Documentation

Using Model Workspaces

About Model Workspaces

Each model is provided with its own workspace for storing variable values. The model workspace is similar to the base MATLAB workspace except that:

Simulink.ModelWorkspace Data Object Class

An instance of this class describes a model workspace. Simulink creates an instance of this class for each model that you open during a Simulink session. The methods associated with this class can be used to accomplish a variety of tasks related to the model workspace, including:

For more information, see the reference page for the Simulink.ModelWorkspace data object class.

Changing Model Workspace Data

The procedure for modifying a workspace depends on the data source of the model workspace.

Changing Workspace Data Whose Source Is the Model File

If the data sources of a model workspace is data stored in the model, you can use Model Explorer (see The Model Explorer: Overview) or MATLAB commands to change the model's workspace (see Using MATLAB Commands to Change Workspace Data).

For example, to create a variable in a model workspace, using Model Explorer, first select the workspace in the Model Explorer Model Hierarchy pane. Then select MATLAB Variable from Model Explorer Add menu or toolbar. You can similarly use the Add menu or toolbar to add a Simulink.Parameter object to a model workspace.

To change the value of a model workspace variable, select the workspace, then select the variable in Model Explorer's Contents pane and edit the value displayed in the Contents pane or in Model Explorer Dialog pane. To delete a model workspace variable, select the variable in the Contents pane and from the Model Explorer Edit menu or toolbar and then select Delete. To save the changes, save the model.

Changing Workspace Data Whose Source Is a MAT-File or MATLAB File

You can also use Model Explorer or MATLAB commands to modify workspace data whose source is a MAT-file or MATLAB file. In this case, if you want to make the changes permanent, you must save the changes to the MAT-file or MATLAB file, using the Save To Source button on the Model Workspace dialog box (see Model Workspace Dialog Box). To discard changes to the workspace, use the Reinitialize From Source button on the Model Workspace dialog box.

Changing Workspace Data Whose Source Is MATLAB Code

The safest way to change data whose source is MATLAB code is to edit and reload the source (that is, edit the MATLAB code and then clear the workspace and re-execute the code, using the Reinitialize From Source button on the Model Workspace dialog box).

To save and reload alternative versions of the workspace that result from editing the MATLAB code source or the workspace variables themselves, see Exporting Workspace Variables and Importing Workspace Variables.

Using MATLAB Commands to Change Workspace Data

To use MATLAB commands to change data in a model workspace, first get the workspace for the currently selected model:

hws = get_param(bdroot, 'modelworkspace');

This command returns a handle to a Simulink.ModelWorkspace object whose properties specify the source of the data used to initialize the model workspace. Edit the properties to change the data source. Use the workspace methods to:

For example, the following MATLAB code creates variables specifying model parameters in the model workspace, saves the parameters, modifies one of them, and then reloads the workspace to restore it to its previous state.

hws = get_param(bdroot, 'modelworkspace');
hws.DataSource = 'MAT-File';
hws.FileName = 'params';
hws.assignin('pitch', -10);
hws.assignin('roll', 30);
hws.assignin('yaw', -2);
hws.saveToSource;
hws.assignin('roll', 35);
hws.reload;

Model Workspace Dialog Box

The Model Workspace dialog box enables you to specify a data source for a model workspace and to specify model reference arguments. To display the dialog box for a model workspace:

  1. Right-click the model workspace in the Model Explorer Model Hierarchy pane.

  2. Select the Properties menu item.

    The dialog box contains a Data source control and a Model arguments (for referencing this model) control.

To use MATLAB commands to change data in a model workspace, see Using MATLAB Commands to Change Workspace Data.

Data source

Specifies the data source of this workspace. The options are

MAT-File and MATLAB File Source Controls

Selecting MAT-File or MATLAB File as the Data source for a workspace causes the Model Workspace dialog box to display additional controls.

File name.  File name or path name of the MAT-file or MATLAB file that is the data source for the selected workspace. If you specify a file name, the name must reside on the MATLAB path.

Reinitialize From Source.  Clears the workspace and reloads the data from the MAT-file or MATLAB file specified by the File name field.

Save To Source.  Save the workspace in the MAT-file or MATLAB file specified by the File name field.

MATLAB Code Source Controls

Selecting MATLAB Code as the Data source for a workspace causes the Model Workspace dialog box to display additional controls.

MATLAB Code.  Specifies MATLAB code that initializes the selected workspace. To change the initialization code, edit this field, then select the Reinitialize from source button on the dialog box to clear the workspace and execute the modified code.

Reinitialize from Source.  Clears the workspace and executes the contents of the MATLAB Code field.

Model Arguments (For Referencing This Model)

This field allows you to specify arguments that can be passed to instances of this model referenced by another model. For more information, see Using Model Arguments.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS