Documentation Center

  • Trial Software
  • Product Updates

clear

Remove items from workspace, freeing up system memory

Syntax

Description

clear removes all variables from the current workspace, releasing them from system memory. When called from within a function with persistent variables, clear reinitializes the persistent variables.

example

clear name1 ... nameN removes the variables, scripts, functions, or MEX-functions name1 ... nameN from memory. clear removes debugging breakpoints in MATLAB files.

  • 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

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

clear -regexp ^Mon ^Tue ^Wed;

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.

ItemType

Items Cleared

all

Removes all variables from the base workspace, and all compiled scripts, functions, and MEX-functions from memory.

If called from a function, clear all clears the function workspace instead of the base workspace.

clear all removes debugging breakpoints in MATLAB files and reinitializes persistent variables. When issued from the Command Window prompt, clear all also removes the Java® packages import list.

classes

The same as clear all, but also clears MATLAB® class definitions. clear classes 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 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 functions, scripts, and MEX-functions from memory. If called from a function, clear functions removes all functions from memory. It also removes debugging breakpoints in the code file and reinitializes persistent variables.

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 —regexp expr1 ... exprN to clear all variables that match any of the regular expressions listed.

import

Removes the packages import list. Use this keyword only from the command prompt. Using this keyword in a function returns an error.

java

The same as clear all, but also clears the definitions of all Java classes defined by files on the Java dynamic class path. If any Java objects exist outside the workspace (for example, in user data or persistent variables in a locked code 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

Clears all MEX-functions from memory, except for locked functions or functions that are currently in use. clear mex also clears breakpoints and persistent variables.

variables

Clears all variables from the workspace.

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 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.

See Also

| | | | | | | | | |

Was this topic helpful?