Main Content

clear

Remove items from workspace, freeing up system memory

Description

clear removes all variables from the current workspace, releasing them from system memory.

clear name1 ... nameN removes the variables, scripts, functions, or MEX functions name1 ... nameN from memory.

example

clear -regexp expr1 ... exprN removes all variables that match any of the regular expressions listed. This option only removes variables.

example

clear ItemType removes the types of items indicated by ItemType, such as all, functions, or classes.

example

Examples

collapse all

Define two variables a and b, and then clear a.

a = 1;
b = 2;
clear a

Only variable b remains in the workspace.

whos
  Name      Size            Bytes  Class     Attributes

  b         1x1                 8  double              

Using regular expressions, clear those variables with names that begin with Mon, Tue, or Wed.

clear -regexp ^Mon ^Tue ^Wed;

Create a cell array, vars, that contains the names of variables to clear. Then, clear those variables.

vars = {'v1','v2','time'};
clear(vars{:})

If a function is locked or currently running, it is not cleared from memory.

clear functions

Input Arguments

collapse all

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, then clear 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, use clear global variable.

Use a partial path to distinguish between different overloaded versions of a function.

Regular expressions matching names of variables to clear, specified as one or more character vectors or string scalars.

Type of items to clear, specified as one of the following.

Value of ItemTypeItems ClearedNotes
Variables in scopeScripts and functionsClass definitionsPersistent variablesMEX functionsGlobal variablesImport listJava 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, and clear 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 or clearvars

    • To clear all global variables, use clear global or clearvars –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.

  • The clear function can remove variables that you specify. To remove all except a few specified variables, use clearvars 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. Use bdclose 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

Version History

Introduced before R2006a