| Using Simulink | ![]() |
Changing Model Workspace Data
The procedure for modifying a workspace depends on the workspace's data source. See the following sections for more information.
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 the 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 the Model Explorer, first select the workspace in the Model Explorer's Model Hierarchy pane. Then select MATLAB Variable from the Model Explorer's Add menu or toolbar. You can similarly use the Add menu or the Model Explorer 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 the Model Explorer's Contents pane and edit the value displayed in the Contents pane or in the Model Explorer's object Dialog pane. To delete a model workspace variable, select the variable in the Contents pane and select Delete from the Model Explorer's Edit menu or toolbar. To save the changes, save the model.
Changinging Workspace Data Whose Source Is a MAT-File
You can also use the 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 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's 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's 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:
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'); hsw.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;
| Working with Model Workspaces | Model Workspace Dialog Box | ![]() |
© 1994-2005 The MathWorks, Inc.