Model Workspaces

Model Workspace Differences from MATLAB Workspace

Each model is provided with its own workspace for storing variable values.

The model workspace is similar to the base MATLAB® workspace except that:

  • Variables in a model workspace are visible only in the scope of the model.

    If both the MATLAB workspace and a model workspace define a variable of the same name, and the variable does not appear in any intervening masked subsystem or model workspaces, the Simulink® software uses the value of the variable in the model workspace. A model's workspace effectively provides it with its own name space, allowing you to create variables for the model without risk of conflict with other models.

  • When the model is loaded, the workspace is initialized from a data source.

    The data source can be a Model file, a MAT-file, a MATLAB file, or MATLAB code stored in the model file. For more information, see Data source.

  • You can interactively reload and save MAT-file, MATLAB file, and MATLAB code data sources.

  • Only Simulink.Parameter and Simulink.Signal objects for which the storage class is set to Auto can reside in a model workspace. You must create all other Simulink data objects in the base MATLAB workspace to ensure the objects are unique within the global Simulink context and accessible to all models.

      Note:   Subclasses of Simulink.Parameter and Simulink.Signal classes, including mpt.Parameter and mpt.Signal objects (Embedded Coder® license required), can reside in a model workspace only if their storage class is set to Auto.

  • In general, parameter variables in a model workspace are not tunable.

    However, you can tune model workspace variables declared as model arguments for referenced models. For more information, see Using Model Arguments.

    Note:   When resolving references to variables used in a referenced model, the variables of the referenced model are resolved as if the parent model did not exist. For example, suppose a referenced model references a variable that is defined in both the parent model's workspace and in the MATLAB workspace but not in the referenced model's workspace. In this case, the MATLAB workspace is used.

Troubleshooting Memory Issues

When you use a workspace variable as a block parameter, Simulink creates a copy of the variable during the compilation phase of the simulation and stores the variable in memory. This can cause your system to run out of memory during simulation, or in the process of generating code. Your system might run out of memory if you have:

  • Large models with many parameters

  • Models with parameters that have a large number of elements

This issue does not affect the amount of memory that is used to represent parameters in generated code.

Simulink.ModelWorkspace Data Object Class

An instance of the Simulink.ModelWorkspace 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:

  • Listing the variables in the model workspace

  • Assigning values to variables

  • Evaluating expressions

  • Clearing the model workspace

  • Reloading the model workspace from the data source

  • Saving the model workspace to a specified MAT-file or MATLAB file

  • Saving the workspace to the MAT-file or MATLAB file that the workspace designates as its data source

Change Model Workspace Data

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

Change 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 or MATLAB commands to change the model's workspace (see Use MATLAB Commands to Change Workspace Data).

For example, to create a variable in a model workspace:

  1. Open the Model Explorer by selecting View > Model Explorer.

  2. In the Model Explorer Model Hierarchy pane, select the model workspace.

  3. Select Add > MATLAB Variable.

    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:

  1. Open the Model Explorer by selecting View > Model Explorer.

  2. In the Model Explorer Model Hierarchy pane, select the model workspace.

  3. In the Contents pane, select the variable.

  4. In the Contents pane or in Dialog pane, edit the value displayed.

To delete a model workspace variable:

  1. Open the Model Explorer by selecting View > Model Explorer.

  2. In the Model Explorer Model Hierarchy pane, select the model workspace.

  3. In Contents pane, select the variable.

  4. Select Edit > Delete.

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

You can use Model Explorer or MATLAB commands to modify workspace data whose source is a MAT-file or MATLAB file.

To make the changes permanent, in the Model Workspace dialog box, use the Save To Source button to save the changes to the MAT-file or MATLAB file.

  1. Open the Model Explorer by selecting View > Model Explorer.

  2. In the Model Explorer Model Hierarchy pane, right-click the workspace.

  3. Select the Properties menu item.

  4. In the Model Workspace dialog box, use the Save To Source button to save the changes to the MAT-file or MATLAB file.

To discard changes to the workspace, in the Model Workspace dialog box, use the Reinitialize From Source button.

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. Edit the MATLAB code and then in the Model Workspace dialog box, use Reinitialize From Source button to clear the workspace and re-execute the code.

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

Use 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:

  • List, set, and clear variables

  • Evaluate expressions in the workspace

  • Save and reload the workspace

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;

Specify Data Sources

To specify a data source for a model workspace, in the Model Explorer, use the Model Workspace dialog box. To display the dialog box for a model workspace:

  1. Open the Model Explorer by selecting View > Model Explorer.

  2. In the Model Hierarchy pane, right-click the model workspace.

  3. Select the Properties menu item, which opens the Model Workspace dialog box.

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

Data source

The Data source field in the Model Workspace dialog box includes the following data source options for a workspace:

  • Model File

    Specifies that the data source is the model itself.

  • MAT-File

    Specifies that the data source is a MAT file. Selecting this option causes additional controls to appear (see MAT-File and MATLAB File Source Controls).

  • MATLAB File

    Specifies that the data source is a MATLAB file. Selecting this option causes additional controls to appear (see MAT-File and MATLAB File Source Controls).

  • MATLAB Code

    Specifies that the data source is MATLAB code stored in the model file. Selecting this option causes additional controls to appear (see MATLAB Code Source Controls).

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.  Specifies the 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.  Saves 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)

Specifies arguments that can be passed to instances of a model that another model references. For more information, see Using Model Arguments.

Was this topic helpful?