clearvars

Delete all or specified variable(s) from memory in the current session of the application. When no variable names are specified, delete all variables. When -except is specified, exclude the specified variable names from deletion.

Syntax

clearvars

clearvarsx1, x2...

clearvars('-global')

clearvars -excepty1, y2...

Inputs

x1, x2... (optional)
If not specified, delete all variables. If names are specified, the variables with those names are deleted. Names can contain a wildcard '*' pattern in which case the variables with names that match the pattern are deleted.
Type: string
-global (optional)
Argument that deletes global variables from memory, instead of local variables.
Type: Command name.
-except y1, y2... (optional)
Argument that excludes from deletion the variable names following this argument. Variable names can contain a wildcard '*' pattern, which excludes deletion of all variables with names that match the pattern.
Type: Command name.

Examples

Deletes all local variables from memory:
global globalVariable;
testInteger = 24;
testString = 'This is a test string';
clearvars
who
Variables in current scope:

globalVariable
Deletes only global variables from memory:
global globalVariable;
testInteger = 24;
testString = 'This is a test string';
clearvars('-global')
who
Variables in current scope:
testInteger
testString
Delete variable associated with the given name from memory:
global globalVariable;
testInteger = 24;
testString = 'This is a test string';
clearvars testInteger
who
Variables in current scope:
globalVariable
testString
Delete variables associated with names, matching the given pattern from memory:
global globalVariable;
testInteger = 24;
testString1 = 'This is string 1';
testString2 = 'This is an example for the command clearvars';
clearvars *ring*
who
Variables in current scope:
globalVariable
testInteger
Delete variables associated with names, matching the given pattern from memory, using -except to exclude some names from deletion:
global globalVariable;
testInteger = 24;
testString1 = 'This is string 1';
testString2 = 'This is an example for the command clearvars';
clearvars test* -except *g2
who
Variables in current scope:
globalVariable
testString2

Comments

If no arguments are passed, all variables are deleted from memory. If an optional argument, -global, is passed, only global variables are deleted from memory. If an optional argument x is passed, the variable associated with the name x will be deleted. x may also contain a wildcard pattern '*', which will delete all variables with names matching the wildcard pattern from memory.