| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
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, releasing them from system memory.
clear name removes just the M-file or MEX-file function or variable name from your base workspace. If called from a function, clear name removes name from both the function workspace and in your base workspace. You can use wildcards (*) to remove items selectively. For example, clear my* removes any variables whose names begin with the string my. Clearing removes debugging breakpoints in M-files and reinitializes persistent variables. If name is global, clear removes it from the current workspace, but it remains 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 as global. Use clear global name to remove a global variable completely.
clear -regexp expr1 expr2 ... clears all variables that match any of the regular expressions listed. This option only clears variables.
clear global -regexp expr1 expr2 ... clears all global variables that match any of the regular expressions listed.
clear keyword clears the items indicated by keyword. See the following section, Inputs, for a list of keywords and their descriptions.
clear('name1','name2','name3',...) is the function form of the syntax. Use this form for variable names and function names stored in strings.
If you name a variable all, classes, functions, java, import, or variables, calling clear followed by that name deletes the variable with that name. clear does not interpret the name as a keyword in this context. For example, if the workspace contains variables a, all, b, and ball, clear all deletes the variable all only.
You can clear the handle of a figure or other object, but that does not remove the object itself. Use delete to remove objects and files. Deleting an object does not delete the variable, if any, used for storing its handle.
On UNIX 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.
keyword |
A name specifying whether to clear variables, classes, or packages or subsets of them, as described in the following table: |
Keyword | Items Cleared |
|---|---|
all | Removes all functions, variables, global variables, and MEX-files from your base workspace. If called from a function, clear all also clears the function workspace, leaving both workspaces empty. clear all removes debugging breakpoints in M-files and reinitializes persistent variables. 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. This variant issues a warning and does not remove class definitions for objects outside the workspace (for example, in user data or persistent variables in a locked M-file). Call clear classes whenever you change a class definition, including when the number or names of properties, methods, or events or any of their attributes change. |
functions | Clears all the currently compiled M-functions and MEX-functions from memory. If called from a function, clear function removes all functions in both the function workspace and in your base workspace. It also removes debugging breakpoints in the function M-file and reinitializes persistent variables. |
global | Removes all global variables in both the function workspace and in your base workspace. If called from a function, clear global removes all global variables in both the function workspace and in your base workspace. |
import | Removes the Java packages import list. Use this keyword only from the Command prompt; do not use it 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 for more information). If any Java objects exist outside the workspace (for example, in user data or persistent variables in a locked M-file), clear issues a warning and does not remove the Java class definition. Issue a clear java command after modifying any files on the Java dynamic class path. |
mex | The same as clear functions, but clears only MEX-functions, other than locked functions or functions that are currently in use. It also clears breakpoints and persistent variables. |
variables | Clears all variables from the workspace. |
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 following case, 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. munlock testfun % Once you unlock the function from memory, you can clear it. clear functions inmem ans = Empty cell array: 0-by-1
Clearing handle graphics handles does not remove the objects themselves, nor does deleting the objects remove variables storing their handles.
hf = figure; % Creates figure object, stores handle in variable hf delete(hf) % Removes figure object, but not the variable hf clear hf % Removes hf from the workspace; figure could still exist
As an alternative to the clear function, use Edit > Clear Workspace in the MATLAB desktop.
clc | clearvars | close | delete | import | inmem | load | memory | mlock | munlock | pack | persistent | save | who | whos | workspace
![]() | clc | clearvars | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |