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 name, then clear name reinitializes any persistent variables in the function.

  • If function name is locked by mlock, then it remains in memory.

  • If variable name is global, then clear removes it from the current workspace, but it remains in the global workspace.

example

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

example

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

Examples

expand 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, it will not be cleared from memory.

Input Arguments

expand 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 variablesDebugging breakpointsMEX 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:

  • clear all also removes the import list when issued from the command prompt.

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.

  • Call clear classes whenever you change a class definition.

global:

  • clear global removes all global variables in the base and global workspaces. If called from a function, clear global also removes all global variables in the function 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.

Was this topic helpful?