| MATLAB® | ![]() |
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:
To open the Workspace browser, select Desktop > Workspace in the MATLAB desktop, or type workspace at the Command Window prompt.
The Workspace browser opens.

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:
You can display additional columns, including size (dimensions), size in bytes, and other common statistical calculations such as mode and standard deviation. To show or hide columns, select View > Choose Columns or right-click any column header. To specify the size limit for calculations and how NaNs are considered, use Preferences for the Workspace Browser.
To resize a column of information, drag the column header border. To reorder columns, drag a column header to a new position.
You can select the column on which to sort as well as reverse the sort order of any column. Click a column header to sort on that column. Click the column header again to reverse the sort order in that column. For example, to sort on Name, click the column header once. To change from ascending to descending, click the header again. You cannot sort by the Value column in the Workspace browser.
You can directly edit variable values in the Workspace browser Value column. To edit a value, position the pointer in the Value column at the row you want to edit, click, and type the new value.
To view more of the data for a variable, as well as to more easily edit it, double-click a variable name and it opens in the Variable Editor. For more information, see Viewing and Editing Workspace Variables with the Variable Editor.
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.
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.
Note The .mat extension is also used by Microsoft® Access™ software. You can change the default file association in Microsoft® Windows® to associate MAT-files with either MATLAB or Access™. |
To save all of the workspace variables using the Workspace browser:
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.
Specify the location and File name. MATLAB automatically supplies the .mat extension.
The workspace variables are saved under the MAT-file name you specified.
To save some but not all of the current workspace variables:
Select the variable in the Workspace browser. To select multiple variables, Shift+click or Ctrl+click.
Right-click, and from the context menu, select Save As.
The Save to MAT-File dialog box opens.
Specify the location and File name. MATLAB automatically supplies the .mat extension.
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.
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.
To load saved variables into the workspace,
Click the Import Data button
on the toolbar in the Workspace browser.
The Import Data dialog box opens.
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.
Use load to open a saved workspace. For example,
load('june10')
loads all workspace variables from the file june10.mat.
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.
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.
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.
You can delete a variable, which removes it from the workspace:
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.
Press the Delete key on
your keyboard or click the Delete button
on the Workspace browser toolbar.
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.
Use the clear function to clear variables from the workspace. For example,
clear A M
clears the variables A and M from the workspace.
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.
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.
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.
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.

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.
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.
![]() | Workspace, Search Path, and File Operations | Viewing and Editing Workspace Variables with the Variable Editor | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |