Documentation

clear

Remove items from workspace, freeing up system memory

Syntax

Description

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

example

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

If name is

  • A function, then clear name reinitializes any persistent variables in the function

  • A script or function that is currently executing, then it is not cleared

  • A function locked by mlock, then it remains in memory

  • A variable that is global, then clear removes it from the current workspace, but it remains in the global workspace

example

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

example

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

Examples

collapse all

Clear a Single Variable

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

a = 1;
b = 2;
clear a
whos
Name      Size            Bytes  Class     Attributes

b         1x1                 8  double    

Only variable b remains in the workspace.

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 List of Variables

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

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

Clear All Compiled Scripts, Functions, and MEX-functions

clear functions

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

Input Arguments

collapse all

name1 ... nameN — Names of variables, scripts, functions, or MEX-functions to clearstring

Names of variables, scripts, functions, or MEX-functions to clear, specified as strings.

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.

expr1 ... exprN — Regular expressions matching names of variables to clearstring

Regular expressions matching names of variables to clear, specified as strings.

ItemType — Type of items to clearall | classes | functions | global | import | java | mex | variables

Type of items to clear, specified as one of the following strings. Click the linked strings below for additional information.

Value of ItemTypeItems Cleared
Variables in scopeScripts and functionsClass definitionsPersistent variablesMEX functionsGlobal variablesImport listJava classes on the dynamic path

all

 From command prompt only 

classes

 

functions

     

global

       

import

       

java

  

mex

       

variables

       

The following applies to clearing types of items.

all:

  • Calling clear all 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 function or script, use clear myFun.

classes:

  • Calling clear classes decreases code performance and is usually unnecessary. If you modify a class definition, MATLAB® automatically updates it.

    • To instantiate an object with an updated class definition, it is not necessary to clear classes.

    • To clear a particular class, use clear myClass.

    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.

    • Use clear functionName to clear individual functions that need to be refreshed.

    • Use clear mex to clear all MEX functions.

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.

  • Use clear global name1 ... nameN to clear the global variables with the specified names.

  • Use clear global —regexp expr1 ... exprN to clear all global variables that match any of the regular expressions listed.

import:

  • Call clear import only from the command prompt. Calling clear import in a function returns an error.

java:

  • clear java issues a warning and does not remove the Java class definition if any 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.

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.

More About

expand all

Tips

  • The clear function can remove variables that you specify. To remove all but some specified variables, use clearvars instead.

  • You can clear the handle of a figure or graphics object, but the object itself is not removed. Use delete to remove objects. Deleting an object does not delete the variable (if any) used for storing its handle.

  • The clear function does not clear Simulink® models. Use bdclose instead.

  • On UNIX® systems, clear does not affect the amount of memory allocated to the MATLAB process.

Introduced before R2006a

Was this topic helpful?