| Simulink® | ![]() |
| 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's 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 model's MDL-file, a MAT-file, or M-code stored in the model file (see Data source for more information).
You can interactively reload and save MAT-file and M-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 software 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 (Real-Time Workshop® 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 (see Using Model Arguments for more information).
Note When resolving references to variables used in a referenced model, the referenced model's variables 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, the Simulink software 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
This issue does not affect the amount of memory that is used to represent parameters in generated code. |
The procedure for modifying a workspace depends on the workspace's data source.
If a model workspace's data source is data stored in the model, you can use Model Explorer (see The Model Explorer) 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 Model Explorer's Model Hierarchy pane. Then select MATLAB Variable from Model Explorer's Add menu or toolbar. You can similarly use the Add menu or Model Explorer's 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's object Dialog pane. To delete a model workspace variable, select the variable in the Contents pane and select Delete from Model Explorer's Edit menu or toolbar. 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. In this case, if you want to make the changes permanent, you must save the changes to the MAT-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 M-code is to edit and reload the source, i.e., edit the M-code and then clear the workspace and reexecute the code, using the Reinitialize From Source button on the Model Workspace dialog box. You can use the Export to MAT-File and Import From MAT-file buttons to save and reload alternative versions of the workspace that result from editing the M code source or the workspace variables themselves.
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's methods to list, set, and clear variables, evaluate expressions in, and save and reload the workspace.
For example, the following MATLAB sequence of commands creates variables specifying model parameters in the model's 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 model workspace's source and model reference arguments (See Referencing a Model.) To display the dialog box, select the model workspace in Model Explorer's Model Hierarchy pane. To use MATLAB commands to change data in a model workspace, see Using MATLAB® Commands to Change Workspace Data.

The dialog box contains the following controls.
Specifies the source of this workspace's data. The options are
Mdl-File
Specifies that the data source is the model itself. Selecting this option causes additional controls to appear (see MDL-File Source Controls).
MAT-File
Specifies that the data source is a MAT file. Selecting this option causes additional controls to appear (see MAT-File Source Controls).
M-code
Specifies that the data source is M code stored in the model file. Selecting this option causes additional controls to appear (see M-Code Source Controls).
Selecting Mdl-File as the Data source for a workspace causes the Model Workspace dialog box to display additional controls.

Import From MAT-File. This button lets you import data from a MAT-file. Selecting the button causes a file selection dialog box to be displayed. Use the dialog box to select the MAT file that contains the data you want to import.
Export To MAT-File. This button lets you save the selected workspace as a MAT-file. Selecting the button displays a file selection dialog box. Use the dialog box to select the MAT file to contain the saved data.
Clear Workspace. This button clears all data from the selected workspace.
Selecting MAT-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 that is the data source for the selected workspace. If 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 specified by the File name field.
Save To Source. Save the workspace in the MAT-file specified by the File name field.
Import From MAT-File. Loads data from a specified MAT file into the selected model workspace without first clearing the workspace. Selecting this option causes a file selection dialog box to be displayed. Use the dialog box to enter the name of the MAT-file that contains the data to be imported.
Export To MAT-File. Saves the data in the selected workspace in a MAT-file. Selecting the button causes a file selection dialog box to be displayed. Use the dialog box to select the MAT file to contain the saved data.
Clear Workspace. Clears the selected workspace.
Selecting M-Code as the Data source for a workspace causes the Model Workspace dialog box to display additional controls.

M-Code. Specifies M-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 M-Code field.
Import From MAT-File. Loads data from a specified MAT file into the selected model workspace without first clearing the workspace. Selecting this option causes a file selection dialog box to be displayed. Use the dialog box to enter the name of the MAT-file that contains the data to be imported.
Export To MAT-File. Saves the data in the selected workspace in a MAT-file. Selecting the button causes a file selection dialog box to be displayed. Use the dialog box to select the MAT file to contain the saved data.
Clear Workspace. Clears the selected workspace.
This field allows you to specify arguments that can be passed to instances of this model referenced by another model. See Referencing a Model and Using Model Arguments for more information.
![]() | Using Callback Functions | Resolving Symbols | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |