Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

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.

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.

You can specify which buttons you want to appear on the toolbar using preferences. Select File > Preferences > Toolbars to open the dialog box. Click Help for information using the dialog box.

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.

Another way to save selected variables from the Workspace browser to a MAT-file is by dragging the selected variables from the Workspace browser to the Current Folder browser. For more information, see Creating and Updating MAT-Files.

Specifying the Format When Saving MAT-Files

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 folder. 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 Exporting Data.

Viewing and Loading a Saved Workspace and Importing Data

You can view saved variables and load the variables and other data into the workspace using the Current Folder browser, the Import Wizard, or the load function.

Viewing Variables in MAT-Files and Loading Them into the Workspace

Use the Current Folder browser to view the contents of a MAT-file without loading the file into MATLAB. You can also load selected variables by dragging them from a MAT-file in the Current Folder browser to the Workspace browser. For details, see Viewing Details About Files and Folders In the Current Folder Browser.

Function Alternative.   Use whos with the -file option.

Importing Data

MATLAB provides an Import Wizard to load MAT-files and other forms of data into the workspace. This procedure briefly describes how to use the Import Wizard from the Workspace browser to import data from MAT-files.

  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. Any variables in the MAT-file that are not in the workspace are added to the workspace. If any variables being loaded have the same names as variables in the current workspace, MATLAB asks if you want to replace the values in the current workspace with the values in the MAT-file, or cancel.

You can also use the Workspace browser to import data you previously copied to the clipboard by selecting Edit > Paste to workspace, or use Ctrl+V. This imports the clipboard data using the Import Wizard.

For more information about the Import Wizard, see Using the Import Wizard.

Function Alternative for Loading Variables

Use load to open a saved workspace. For example,

load('june10')

loads all workspace variables from the file june10.mat.

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.

Use the clearvars function with the -except option to keep the specified variables and clear all other variables.

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 debugging M-files, use the Stack field in the Workspace browser. The Stack field is only available in debug mode and otherwise appears dimmed. The Stack field is also accessible from the Editor and the Variable Editor. For more information, see Finding Errors, Debugging, and Correcting M-Files. See also the dbstack and evalin functions.

Creating Plots from the Workspace Browser

You can generate a plot of one or more variables with the Plot Selector tool on the Workspace browser toolbar. (The appearance of the tool varies, depending on the variables you select and your history of using it.) The Plot Selector contains menu items identifying plotting functions available to you and executes them when you click the graph icons. The following steps illustrate how the tool works:

  1. Create two vector variables:

    run = 0:.1:4*pi;
    wave = (sin(run.^2) + cos(run).^2);
    
  2. Select one or both variables in the Workspace Browser. You can Shift+click or Ctrl+click to select multiple variables to plot as x, y, or z components of a graph.

  3. Click the down arrow icon in the Plot Selector on the toolbar. The Plot Selector menu opens.

  4. Scroll to the graph type you want to display and click the icon on its left side. The icon highlights when you hover over it.

    A figure window opens with a graph of the selected variable or variables, using the plotting function you just chose.

    The Plot Selector GUI remains open until you click a desktop component or another window. This enables you to experiment with plot types without reopening the GUI each time.

Working with the Plot Selector GUI

You can interact with the Plot Selector in many ways. Learn about using it by viewing this video demo. Also, read the following table to better understand what you can accomplish with the Plot Selector and how to do it.

What You Can DoHow To Do It
Identify variables to graph.Select a variable of numeric type in the Workspace Browser. To add variables to your selection, Shift+click or Ctrl+click.
Plot selected variables immediately using the default graph type.Click the Plot Selector icon; a graph of the type it displays will plot using the selected variables.
Plot selected variables after choosing a graphing function.Click the arrow to the right of the Plot Selector button. The GUI opens for you to choose a graph type. Create plot by clicking the chosen item's picture icon.
Interchange two variables before graphing them.Click the Reverse Input Variable Order button . The two function arguments the item displays interchange. The button appears only when you select two variables.
Identify the graphing functions you can currently use to create a plot.With one or more selected variables, select the first tab, Plots for:<variables> and do either of the following:
  • Scroll through the menu or navigate through it with up/down arrow keys.

  • Enter a search term in the search bar; only those menu items and categories containing the search string will display.

Identify available graphing functions.With zero or more variables selected, select the second tab, All plots and do either of the following:
  • Scroll through the menu or navigate through it with up/down arrow keys

  • Enter a search term in the search bar; only those menu items and categories containing the search string display

Add a graph type to Favorites.In either tab:
  • Click the star to the right of a menu item and do any of the following:

  • Right-click a menu item and select Add to Favorites.

  • Right-click a menu item and select Copy,and then right-click within Favorites and select Paste.

  • Drag a menu item from its category to Favorites.

All of these actions create a copy the item in Favorites without removing it from its category.

Remove a graph type from Favorites.In the Favorites category of either tab do either of the following:
  • Click the star at the right side of a menu item

  • Right-click a menu item and select Remove Favorite

You cannot drag a menu item out of Favorites.

Change the order of menu items within a category.Click a menu item outside the graph icon and drag it to a new position within its category.
Move a menu item to a different category.Click a menu item outside the graph icon and drag it to a new category. Dragging an item away removes that item from the category it was in. This gesture does not apply to items within Favorites.
Get help for a graphing function.On either tab, hover mouse pointer over a menu item. A syntax description pops up next to the item. To see the complete function reference page, click the More Help link in the description header.
Enable a dimmed menu item.Select one or more variables appropriate for calling the dimmed function. Then, open the Plot Selector tool again. See Selecting Appropriate Variables and Determining What Inputs a Graphing Function Needs.
Manually execute or modify a plotting call.Drag a menu item (even if it is dimmed) to the Command Window or the Editor; the code for that plot displays and you can edit it. See Editing a Plot Selector Graphing Command.
Display the Plot Selector as a window.Open the Plot Selector, click the grab bar at the center top, and drag the GUI to where you want it on the screen. That window closes if you click another MATLAB window. The window remains open if you focus on a window of another application.
Access the Plot Catalog.Click the Catalog link at the bottom of the Plot Selector tool.

Selecting Appropriate Variables

If you select insufficient or inappropriate variables for a graph type, its menu item is dimmed on the All plots Plot Selector tab. The dimmed items do not appear at all on the Plots for: tab. Dimmed and missing Plot Selector menu items indicate that you cannot use the function for plotting the selected data via the tool. Reasons why a function is not available include:

If you select variables that no function can display, the Plot Selector button label changes to No valid plots for:<variable> . If you click the button, you see the message "No plot available for selection. Change your variable selection or click the All plots tab to browse for plots."

Determining What Inputs a Graphing Function Needs

When a Plot Selector menu item is unavailable (dimmed) in the All plots tab, you can learn why by viewing the pop-up help for that function. Suppose you want to make a semilogy graph.

In the All plots tab, you can do several different things:

  1. Create a nonnumeric variable, for example, a string.

    name = 'abcd';
  2. Select name in the Workspace Browser and open the Plot Selector tool by clicking its down arrow.

  3. Click the All plots tab in the Plot Selector window.

  4. Scroll to the semilogy menu item (all items are dimmed) and hover the mouse pointer over the item.

    A help message appears that includes a note specifying what inputs the semilogy function accepts. The pop-up help window contains a white box that describes the function's input requirements, as shown in the following figure.

    The message helps you to understand what arguments the semilogy function supports.

A Help Window opens whenever your mouse pointer lingers over a menu item, but information set off in a white box only appears when the function is dimmed because it cannot generate a graph of the currently selected variables.

Editing a Plot Selector Graphing Command

Even when the Plot Selector tool cannot successfully make a graph, it can still give you a starting point for doing so. You can drag any Plot Selector menu item into the Command Window to see the code it generates. Once the plotting command displays in the Command Window, it does not execute until you press Enter, enabling you to edit its arguments first. Dragging dimmed items works the same as dragging undimmed items. This enables you to fix problems due to selecting the wrong variables or selecting them in the wrong sequence. You can also add arguments to the plotting command, for example to specify a linespec or a colorspec for functions that can use them.

The following example illustrates how to drag a dimmed Plot Selector menu item, drop it into the Command Window, and edit the plotting command before executing it.

  1. In the Command Window, enter

    alist = '1 2 3 4 5 6 7 8 9 10';
    ilist = [1 2 3 4 5 6 7 8 9 10];
  2. Assume that you want to create a graph of ilist but instead you select the string variable alist in the Workspace Browser. When you click the Plot Selector, it displays this error message.

    You can go on to plot ilist by continuing as follows.

  3. Click the All Plots tab and scroll to semilogy. The menu item is dimmed.

  4. Press the mouse button over semilogy and drag it to the command window, as shown here.

  5. Release the mouse button in the Command Window The following line of code appears there:

    >> semilogy(alist,'DisplayName','alist');figure(gcf)

    If you press Enter, an error displays because alist is not a proper calling argument:

    ??? Error using ==> semilogy
    Invalid first data argument
    
  6. Instead, change the code to the following, substituting ilist for alist:

    semilogy(ilist,'DisplayName','ilist');figure(gcf)

    This call works with the changed arguments and generates the following graph after you press Enter.

    You can also edit the plotting command to add other arguments, including parameter/value pairs, to customize the graph.

For More Information

If you want to use the Plot Selector to create a graph for a subrange of a vector or a matrix, you can open the variable in the Variable Editor, select the range of data you want to graph, and open the Plot Selector from the Variable Editor toolbar. All data for the graph must all come from one variable and the subrange to plot has to be a contiguous selection.

You can add titles, axis labels, legends and other annotations to graphs that the Plot Selector makes. For more information about graphing data and customizing plots, see the following topics in 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.

Setting Workspace Browser Preferences

The Workspace browser displays statistical calculations for variables. Use preferences to restrict the size of arrays on which you perform calculations and to specify if you want those calculations to include or ignore NaNs. Select File > Preferences > Workspace to open the dialog box. Make your 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.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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