clearvars

Clears selected variables from the workspace, with options to keep some.

Category: Data Import and Analysis > Data Import and Export

Syntax

clearvars

Clears every variable.

clearvars x y

Clears only the named variables. Patterns with * are allowed.

clearvars -except x y

Clears everything except the listed variables.

Description

clearvars works only on variables, unlike clear, which also resets functions and other state. The -except form is the quickest way to clean a workspace after an experiment while holding on to results.

Input arguments

-except
Keyword that turns the list into variables to keep.

Example

Keep one variable

a = 1; b = 2; c = 3;
clearvars -except b
exist('a')
exist('b')
exist('c')
Output
ans = 0
ans = 1
ans = 0

See also

clear exist who whos