| MATLAB® | ![]() |
As an alternative to the clear function, use Edit > Clear Workspace in the MATLAB desktop.
clear
clear name
clear name1 name2 name3 ...
clear global name
clear -regexp expr1
expr2 ...
clear global -regexp expr1
expr2 ...
clear keyword
clear('name1','name2','name3',...)
clear removes all variables from the workspace. This frees up system memory.
clear name removes just the M-file or MEX-file function or variable name from the workspace. You can use wildcards (*) to remove items selectively. For example, clear my* removes any variables whose names begin with the string my. It removes debugging breakpoints in M-files and reinitializes persistent variables, since the breakpoints for a function and persistent variables are cleared whenever the M-file is changed or cleared. If name is global, it is removed from the current workspace, but left accessible to any functions declaring it global. If name has been locked by mlock, it remains in memory.
Use a partial path to distinguish between different overloaded versions of a function. For example, clear polynom/display clears only the display method for polynom objects, leaving any other implementations in memory.
clear name1 name2 name3 ... removes name1, name2, and name3 from the workspace.
clear global name removes the global variable name. If name is global, clear name removes name from the current workspace, but leaves it accessible to any functions declaring it global. Use clear global name to completely remove a global variable.
clear -regexp expr1 expr2 ... clears all variables that match any of the regular expressions expr1, expr2, etc. This option only clears variables.
clear global -regexp expr1 expr2 ... clears all global variables that match any of the regular expressions expr1, expr2, etc.
clear keyword clears the items indicated by keyword.
Keyword | Items Cleared |
|---|---|
all | Removes all variables, functions, and MEX-files from memory, leaving the workspace empty. Using clear all removes debugging breakpoints in M-files and reinitializes persistent variables, since the breakpoints for a function and persistent variables are cleared whenever the M-file is changed or cleared. When issued from the Command Window prompt, also removes the Sun Microsystems Java packages import list. |
classes | The same as clear all, but also clears MATLAB class definitions. If any objects exist outside the workspace (for example, in user data or persistent variables in a locked M-file), a warning is issued and the class definition is not cleared. Issue a clear classes function if the number or names of fields in a class are changed. |
functions | Clears all the currently compiled M-functions and MEX-functions from memory. Using clear function removes debugging breakpoints in the function M-file and reinitializes persistent variables, since the breakpoints for a function and persistent variables are cleared whenever the M-file is changed or cleared. |
global | Clears all global variables from the workspace. |
import | Removes the Java packages import list. It can only be issued from the Command Window prompt. It cannot be used in a function. |
java | The same as clear all, but also clears the definitions of all Java classes defined by files on the Java dynamic class path (see The Java Class Path in the External Interfaces documentation). If any Java objects exist outside the workspace (for example, in user data or persistent variables in a locked M-file), a warning is issued and the Java class definition is not cleared. Issue a clear java command after modifying any files on the Java dynamic class path. |
variables | Clears all variables from the workspace. |
clear('name1','name2','name3',...) is the function form of the syntax. Use this form when the variable name or function name is stored in a string.
When you use clear in a function, it has the following effect on items in your function and base workspaces:
clear name — If name is the name of a function, the function is cleared in both the function workspace and in your base workspace.
clear functions — All functions are cleared in both the function workspace and in your base workspace.
clear global — All global variables are cleared in both the function workspace and in your base workspace.
clear all — All functions and global variables are cleared in both the function workspace and in your base workspace.
On UNIX[1] systems, clear does not affect the amount of memory allocated to the MATLAB process.
The clear function does not clear Simulink models. Use close instead.
Given a workspace containing the following variables
Name Size Bytes Class c 3x4 1200 cell array frame 1x1 java.awt.Frame gbl1 1x1 8 double array (global) gbl2 1x1 8 double array (global) xint 1x1 1 int8 array
you can clear a single variable, xint, by typing
clear xint
To clear all global variables, type
clear global whos Name Size Bytes Class c 3x4 1200 cell array frame 1x1 java.awt.Frame
Using regular expressions, clear those variables with names that begin with Mon, Tue, or Wed:
clear('-regexp', '^Mon|^Tue|^Wed');To clear all compiled M- and MEX-functions from memory, type clear functions. In the case shown below, clear functions was unable to clear one M-file function from memory, testfun, because the function is locked.
clear functions % Attempt to clear all functions.
inmem
ans =
'testfun' % One M-file function remains in memory.
mislocked testfun
ans =
1 % This function is locked in memory.Once you unlock the function from memory, you can clear it.
munlock testfun clear functions inmem ans = Empty cell array: 0-by-1
clc, clearvars, close, import, inmem, load, mlock, munlock, pack, persistent, save, who, whos, workspace
MATLAB Workspace in the Desktop Tools and Development Environment documentation
[1] is a registered trademark of The Open Group in the United States and other countries
![]() | clc | clearvars | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |