clear

Removes variables from the workspace.

Category: Data Import and Analysis > Data Import and Export

Syntax

clear

Removes every variable from the current workspace.

clear name1 name2

Removes the named variables. Wildcards such as x* are accepted.

clear all

Removes variables and also resets cached functions and state.

clear('name')

Function form, convenient when the name is held in a string.

Description

clear is usually written in command syntax, without parentheses or quotes. Clearing a name that does not exist is not an error. After clearing, using the variable raises the usual undefined-variable error.

To drop everything except a few variables, use clearvars with -except.

Input arguments

name
Variable name or pattern. * matches any run of characters.

Example

Clear by name and pattern

xa = 1; xb = 2; yy = 3;
clear x*
exist('xa')
exist('yy')
clear yy
exist('yy')
Output
ans = 0
ans = 1
ans = 0

See also

clearvars exist who whos