Main Content

clearvars

Clear variables from memory

Description

clearvars removes all variables from the currently active workspace.

clearvars variables removes the variables specified by variables. If any of the variables are global, clearvars removes these variables from the current workspace only, leaving them accessible to any functions that declare them as global.

example

clearvars -except keepVariables removes all variables, except for those specified by keepVariables. Use this syntax to keep specific variables and remove all others.

example

clearvars variables -except keepVariables removes the variables specified by variables, and does not remove the variables specified by keepVariables.

clearvars -regexp variables removes variables with names that match the specified regular expressions.

example

clearvars ___ -except -regexp keepVariables removes variables except those with names that match the specified regular expressions.

example

clearvars -global ___ removes the specified global variables from the workspace, including those made global within functions, using any of the input arguments in the preceding syntaxes. The -global flag must be first in the argument list.

example

Examples

collapse all

Define three variables, a, b, and c. Then, clear a and c.

a = 1;
b = 2;
c = 3;
clearvars a c
whos
  Name      Size            Bytes  Class     Attributes

  b         1x1                 8  double     

Only variable b remains in the workspace.

Remove all variables from the workspace except for the variables C and D.

clearvars -except C D

Clear variables with names that start with b and are followed by 3 digits, except for the variable b106.

clearvars -regexp ^b\d{3}$ -except b106

Clear variables with names that start with a and do not end with a.

clearvars a* -except -regexp a$

Clear all global variables, except those with names that start with x.

clearvars -global -except x*

Clear a list of variables used for intermediate calculations.

Create two variables in the workspace.

cashOnHand = 20;
cost = 12.99;

Store a list of the names of all the variables currently in the workspace.

initialVars = who;

Specify or calculate additional variables, taxRate and tax.

taxRate = 0.0625;
tax = round(100*cost*taxRate)/100;

Update the initial variables, cost and cashOnHand.

cost = cost + tax;
cashOnHand = cashOnHand - cost;

Clear all variables except the initial variables, using the function form of clearvars. When using the function form of a syntax, enclose input character vectors in single quotes, and separate them with commas.

clearvars('-except',initialVars{:})

clearvars clears the variables, initialVars, taxRate, and tax.

Input Arguments

collapse all

Names of variables to remove, specified as one or more character vectors or string scalars in one of these forms.

Form of Variables InputVariables to Remove
var1 ... varNNamed variables.
Use the '*' wildcard to match patterns. For example, clearvars A* clears all variables in the workspace with names that start with A.
-regexp expr1 ... exprNVariables with names that match the regular expressions. For example, clearvars -regexp ^Mon ^Tues clears only the variables in the workspace with names that begin with Mon or Tues.

Data Types: char | string

Names of variables to keep, specified as one or more character vectors or string scalars, in one of these forms.

Form of Variables InputVariables to Keep
var1 ... varNNamed variables.
Use the '*' wildcard to match patterns. For example, clearvars -except A* clears all variables in the workspace, except those with names that start with A.
-regexp expr1 ... exprNVariables with names that match the regular expressions. For example, clearvars -except -regexp ^Mon ^Tues clears all the variables in the workspace, except those with names that begin with Mon or Tues.

Data Types: char | string

Version History

Introduced in R2008a