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

Changing Model Workspace Data

The procedure for modifying a workspace depends on the workspace's data source.

Changing Workspace Data Whose Source Is the Model File

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.

Changing Workspace Data Whose Source Is a MAT-File

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.

Changing Workspace Data Whose Source Is M-Code

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.

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'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;

Model Workspace Dialog Box

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.

Data source

Specifies the source of this workspace's data. The options are

MDL-File 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.

MAT-File Source Controls

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.

M-Code Source Controls

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.

Model Arguments

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.

  


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