Documentation Center

  • Trial Software
  • Product Updates

clearvars

Clear variables from memory

Syntax

  • clearvars
  • clearvars variables example
  • clearvars -except keepVariables example
  • clearvars variables -except keepVariables example

Description

clearvars removes all variables from the currently active workspace.

example

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. This syntax allows you to use a combination of variable names, wild card characters, or regular expressions to specify variables to remove or keep.

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.

Examples

expand all

Clear Named Variables

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.

Clear All Variables Except Specified

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

clearvars -except C D

Clear Variables Using Regular Expressions and Name Variables to Exclude

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

Name Variables to Clear and Preserve Variables Using Regular Expressions

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

clearvars a* -except -regexp a$

Clear Global Variables Except Specified

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

clearvars -global -except x*

Clear List of Variables

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 strings in single quotes, and separate them with commas.

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

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

Input Arguments

expand all

variables — Names of variables to removestrings

Names of variables to remove, specified as one or more strings in one of the following forms.

Form of Variables InputVariables to Remove
var1 ... varNNamed variables, specified as individual strings.
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, specified as strings. For example, clearvars -regexp ^Mon ^Tues clears only the variables in the workspace with names that begin with Mon or Tues.

keepVariables — Names of variables to keepstrings

Names of variables to keep, specified as one or more strings in one of the following forms.

Form of Variables InputVariables to Keep
var1 ... varNNamed variables, specified as individual strings.
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, specified as strings. For example, clearvars -except -regexp ^Mon ^Tues clears all the variables in the workspace, except those with names that begin with Mon or Tues.

See Also

| | | | | |

Was this topic helpful?