Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

라이선스가 부여된 사용자만 번역 문서를 볼 수 있습니다. 번역 문서를 보려면 로그인하십시오.

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

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

Names of variables to keep, specified as one or more character vectors, 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.

See Also

| | | | | |

Introduced in R2008a

Was this topic helpful?