clear - Remove items from workspace, freeing up system memory - MATLAB (original) (raw)
Remove items from workspace, freeing up system memory
Syntax
Description
clear
removes all variables from the current workspace, releasing them from system memory.
clear [name1 ... nameN](#btdyzbv-1-name1nameN)
removes the variables, scripts, functions, or MEX functions name1 ... nameN
from memory.
clear -regexp [expr1 ... exprN](#btdyzbv-1-expr1exprN)
removes all variables that match any of the regular expressions listed. This option only removes variables.
clear [ItemType](#btdyzbv-1-ItemType)
removes the types of items indicated by ItemType
, such as all
, functions
, or classes
.
Examples
Clear a Single Variable
Define two variables a
and b
, and then clear a
.
Only variable b
remains in the workspace.
Name Size Bytes Class Attributes
b 1x1 8 double
Clear Specific Variables by Name
Using regular expressions, clear those variables with names that begin with Mon
, Tue
, or Wed
.
clear -regexp ^Mon ^Tue ^Wed;
Clear Set of Variables
Create a cell array, vars
, that contains the names of variables to clear. Then, clear those variables.
vars = {'v1','v2','time'}; clear(vars{:})
Clear All Compiled Scripts, Functions, and MEX Functions
If a function is locked or currently running, it is not cleared from memory.
Input Arguments
name1 ... nameN
— Names of variables, scripts, functions, or MEX functions to clear
character vectors | string scalars
Names of variables, scripts, functions, or MEX functions to clear, specified as one or more character vectors or string scalars.
If name
is a:
- Function, then
clear
reinitializes any persistent variables in the function. Specifying a local or nested function is not supported. - Script or function that is currently executing or a function locked by
mlock
, thenclear
does not remove it. - Global variable, then
clear
removes it from the current workspace, but it remains in the global workspace. To remove a global variable from all workspaces, useclear global
variable
.
Use a partial path to distinguish between different overloaded versions of a function.
expr1 ... exprN
— Regular expressions matching names of variables to clear
character vectors | string scalars
Regular expressions matching names of variables to clear, specified as one or more character vectors or string scalars.
ItemType
— Type of items to clear
all
| classes
| functions
| global
| import
| java
| mex
| variables
Type of items to clear, specified as one of the following.
Value of ItemType | Items Cleared | Notes | |||||||
---|---|---|---|---|---|---|---|---|---|
Variables in scope | Scripts and functions | Class definitions | Persistent variables | MEX functions | Global variables | Import list | Java classes on the dynamic path | ||
all | ✓ | ✓ | ✓ | ✓ | ✓ | From command prompt only | Calling clear all decreases code performance, and is usually unnecessary. For more information, see the Tips section. | ||
classes | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | Calling clear classes decreases code performance, and is usually unnecessary. For more information, see the Tips section.To instantiate an object with an updated class definition, it is not necessary to call clear classes. When you modify a class definition, MATLAB® updates it automatically. For more information, see Automatic Updates for Modified Classes.clear classes issues a warning and does not clear a class of objects if any of those objects still exists after the workspace is cleared. For example, objects can still exist in persistent variables of functions or figure windows. clear classes does not clear a class if its file is locked using the mlock command. No warning is issued in this case. | |
functions | ✓ | ✓ | ✓ | Calling clear functions decreases code performance, and is usually unnecessary. For more information, see the Tips section. | |||||
global | ✓ | clear global removes all global variables in the current and global workspaces. For example, when called from a function,clear global removes all global variables in the function and global workspaces, but not in the base workspace. | |||||||
import | ✓ | Call clear import only from the command prompt. Calling clear import in a function or a script returns an error. | |||||||
java | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | clear java issues a warning and does not remove the Java class definition if any of its Java objects exist outside the workspace (for example, in user data or persistent variables in a locked code file).Issue a clear java command after modifying any files on the dynamic Java path. | ||
mex | ✓ | clear mex does not clear locked MEX functions or functions that are currently in use. | |||||||
variables | ✓ |
Note
If the name of a variable is a value of ItemType
, then 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
removes the variable all
only.
Tips
- Calling
clear all
,clear classes
, andclear functions
decreases code performance, and is usually unnecessary.- To clear one or more specific variables from the current workspace, use
clear _`name1 ... nameN`_
. - To clear all variables from the current workspace, use
clear
orclearvars
. - To clear all global variables, use
clear global
orclearvars –global
. - To clear a particular class, use
clear _`myClass`_
. - To clear a particular function or script, use
clear _`functionName`_
. - To clear all MEX functions, use
clear mex
.
- To clear one or more specific variables from the current workspace, use
- The
clear
function can remove variables that you specify. To remove all except a few specified variables, useclearvars
instead. - If you clear the handle of a figure or graphics object, the object itself is not removed. Use
delete
to remove objects. On the other hand, deleting an object does not remove the variable (if any) used for storing its handle. - The
clear
function does not clear Simulink® models. Usebdclose
instead. - The
clear
function does not clear persistent variables in local or nested functions. - On UNIX® systems,
clear
does not affect the amount of memory allocated to the MATLAB process.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
- Code generation ignores the
clear
function and it does not affect the generated C/C++ code.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006a