MATLAB® Workspace

About the Workspace

The workspace consists of the set of variables built up during a session of using the MATLAB® software and stored in memory. You add variables to the workspace by using functions, running M-files, and loading saved workspaces. For example, if you run these statements,

A = magic(4)
R = randn(3,4,5)

the workspace includes two variables, A and R.

You can perform workspace and related operations using the Workspace browser. When available, equivalent functions are documented with each feature of the Workspace browser. If you have an active Internet connection, you can watch the Workspace browser video demo for an overview of the major functionality:

Opening the Workspace Browser

To open the Workspace browser, select Desktop > Workspace in the MATLAB desktop, or type workspace at the Command Window prompt.

The Workspace browser opens.

Image of Workspace browser.

Viewing and Editing Values in the Current Workspace

The Workspace browser shows the name of each variable or object, the class (also represented by the icon), its value, and where relevant, the Min, Max, and Mean calculations. MATLAB performs these calculations using the min, max, and mean functions, and updates the results automatically. These are other features of the Workspace browser:

Function Alternative

Use who to list the current workspace variables. Use whos to list the variables and information about size and class. For example:

>> who

Your variables are:

A        S         avg_score  names      scores     v          y
C        a         b          nn         t          w1         z
R        ans       l          s1         td         x          

>> whos
  Name           Size           Bytes  Class          Attributes

  A              4x4              128  double                   
  C              1x3              348  cell                     
  R              3x4x5            480  double                   
  S              1x3              826  struct                   
  a              4x4              128  double                   
  ans            3x4x5            480  double                   
  avg_score      1x1                8  double                   
  b              4x4              128  double                   
  l              4x4               16  logical                  
  names          3x12              72  char                     
  nn             3x3               72  double                   
  s1             1x1                4  single                   
  scores         1x3               24  double                   
  t              1x5               10  char                     
  td             1x1              152  TensileData              
  v              2x5               20  char                     
  w1             1x1               16  double         complex   
  x              1x1                2  int16                    
  y              1x3               12  uint32                   
  z              1x1                8  double               

Use exist to see if the specified variable is in the workspace.

Saving the Current Workspace

The workspace is not maintained across sessions of MATLAB. When you quit MATLAB, the workspace is cleared. You can save any or all of the variables in the current workspace to a MAT-file, which is a binary file specifically for use in MATLAB. You can then load the MAT-file at a later time during the current or another session to reuse the workspace variables. MAT-files use a .mat extension.

Saving All Variables

To save all of the workspace variables using the Workspace browser:

  1. Select File > Save Workspace As from the Workspace browser, or click the Save button in the Workspace browser toolbar.

    The Save to MAT-File dialog box opens.

  2. Specify the location and File name. MATLAB automatically supplies the .mat extension.

  3. Click Save.

    The workspace variables are saved under the MAT-file name you specified.

Saving Selected Variables

To save some but not all of the current workspace variables:

  1. Select the variable in the Workspace browser. To select multiple variables, Shift+click or Ctrl+click.

  2. Right-click, and from the context menu, select Save As.

    The Save to MAT-File dialog box opens.

  3. Specify the location and File name. MATLAB automatically supplies the .mat extension.

  4. Click Save.

    The workspace variables are saved under the MAT-file name you specified.

To specify preferences for saving MAT-files that pertain to compression, and compatibility between different versions of MATLAB, see MAT-Files Preferences.

Function Alternative

To save workspace variables, use the save function followed by the filename you want to save to. For example,

save('june10')

saves all current workspace variables to the file june10.mat.

If you don't specify a filename, the workspace is saved to matlab.mat in the current directory. You can specify which variables to save, as well as control the format in which the data is stored, such as ASCII. For these and other forms of the function, see the reference page for save. MATLAB provides additional functions for saving information — see Data Import and Export in the MATLAB Programming Fundamentals documentation.

Loading a Saved Workspace and Importing Data

To load saved variables into the workspace,

  1. Click the Import Data button on the toolbar in the Workspace browser.

    The Import Data dialog box opens.

  2. Select the MAT-file you want to load and click Open.

    The variables and their values, as stored in the MAT-file, are loaded into the current workspace. If any variables being loaded have the same names as variables in the current workspace, the values from the MAT-file replace the values in the current workspace. Any variables in the MAT-file that are not in the workspace are added to the workspace.

Function Alternative

Use load to open a saved workspace. For example,

load('june10')

loads all workspace variables from the file june10.mat.

Importing Data

MATLAB provides other methods and functions for loading information. One of these methods is available from the Workspace browser, the Import Wizard. You can import data you previously copied to the clipboard by selecting Edit > Paste to workspace or use Ctrl+V. This imports the data using the Import Wizard. For more information on the Import Wizard, see the Using the Import Wizard in the Programming Fundamentals documentation.

Viewing Variables in MAT-Files

Use the Current Directory browser to view the contents of a MAT-file without loading the file into MATLAB. For details, see Current Directory Browser.

Function Alternative.   Use whos with the -file option.

Changing and Copying Variable Names

To rename a variable in the workspace, right-click the variable in the Workspace browser and select Rename from the context menu. Type the new variable name over the existing name and press Enter.

To copy variable names to the clipboard, select the workspace variables and select Edit > Copy. You can then paste the names, for example, into the Command Window. Multiple variables are comma separated.

Deleting Workspace Variables

You can delete a variable, which removes it from the workspace:

  1. In the Workspace browser, select the variable, or Shift+click or Ctrl+click to select multiple variables. To select all variables, choose Select All from the Edit or context menus.

  2. Press the Delete key on your keyboard or click the Delete button on the Workspace browser toolbar.

  3. A confirmation dialog box might appear. If it does, click OK to clear the variables.

    The confirmation dialog box appears if you selected that preference. For more information, see Confirmation Dialogs Preferences.

To delete all variables, select Edit > Clear Workspace from any desktop tool.

Function Alternative

Use the clear function to clear variables from the workspace. For example,

clear A M

clears the variables A and M from the workspace.

Viewing Base and Function Workspaces Using the Stack

When you run M-files, MATLAB assigns each function its own workspace, called the function workspace, which is separate from the base workspace in MATLAB. To access the base and function workspaces when running or debugging M-files, use the Stack field in the Workspace browser. The Stack field is only available in debug mode and otherwise is grayed out. The Stack field is also accessible from the Variable Editor and the Editor/Debugger. See Finding Errors, Debugging, and Correcting M-Filesfor more information. See also the dbstack and evalin functions.

Creating Plots from the Workspace Browser

From the Workspace browser, you can generate a plot of a variable. To create a plot, click the Plot button on the Workspace browser toolbar and select the plot type. The plot appears in a figure window. The button itself changes to reflect the currently selected style of plot, for example bar or stem.

This feature is only available for variables whose classes can be plotted, such as numeric. Open the variable in the Variable Editor for additional plotting options.

In addition, you can right-click the variable you want to plot. From the context menu, choose the type of plot you want to create.

You can also Shift+click or Ctrl+click to select multiple variables to plot together. When one of the variables is named time, t, or T, MATLAB assumes it is the independent variable.

For more information about creating graphs in MATLAB, see the MATLAB® Graphics documentation.

Opening Variables and Objects for Viewing and Editing

In the Workspace browser, double-click a variable and it opens in the Variable Editor where you can view and edit the contents of the variable. See Viewing and Editing Workspace Variables with the Variable Editor for more information.

Some toolboxes allow you to double-click an object in the Workspace browser to open a viewer or other tool appropriate for that object. For details, see the toolbox documentation for that object type.

Preferences for the Workspace Browser

The Workspace browser displays statistical calculations for variables. Use preferences to restrict the size of arrays on which calculations are performed and to specify if NaNs are included or ignored in calculations. Select File > Preferences to open the dialog box. Make changes and click OK.

Specify Maximum Array Size on Which to Compute Statistics

If you show statistical columns in the Workspace browser, and if you work with very large arrays, you might experience performance issues when the data changes as MATLAB updates the statistical results. In that event, show only the columns of interest to you and hide those you do not need.

Another step you can take is specify via a preference that the Workspace browser not perform statistical calculations on the largest arrays. Use the arrows to change the value of the maximum array size for which you want the Workspace browser to perform statistical calculations. The default value is 500,000 elements. Any variable exceeding that size reports <Too many elements> instead of statistical results.

Handling NaN Values in Calculations

If your data includes NaNs, you can specify that the statistical calculations consider the NaNs or ignore the NaNs. For example, if a variable includes a NaN and the preference is set to Use NaNs when calculating statistics, the values for Min, Max, Var and some others will appear as NaN, although Mode, for example, shows a numeric result. With the preference set to Ignore NaNs whenever possible, numeric results appear for most of the statistical columns including Min and Max; Var, however, is still reported as NaN.

For more information about statistical values in the Workspace browser, see Viewing and Editing Values in the Current Workspace.

  


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