| Contents | Index |
| On this page… |
|---|
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 the MDL-file for the model, a MAT-file, a MATLAB file, or MATLAB code stored in the model file (see Data source for more information).
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. (See Referencing a Model.) |
Note 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:
This issue does not affect the amount of memory that is used to represent parameters in generated code. |
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:
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
For more information, see the reference page for the Simulink.ModelWorkspace data object class.
The procedure for modifying a workspace depends on the data source of the model workspace.
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.
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.
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.
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;
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:
Right-click the model workspace in the Model Explorer Model Hierarchy pane.

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.
Specifies the data source of this workspace. The options are
Mdl-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).
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.
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.
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.
![]() | Using Callback Functions | Resolving Symbols | ![]() |

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 |