| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
As an alternative to the clearvars function, in the Workspace browser, select variables to clear and then press Delete.
clearvars v1 v2 ...
clearvars -global
clearvars -global v1 v2 ...
clearvars -regexp p1 p2 ...
clearvars -except v1 v2 ...
clearvars -except -regexp p1 p2 ...
clearvars v1 v2 ... -except -regexp p1 p2 ...
clearvars -regexp p1 p2 ... -except v1 v2 ...
clearvars v1 v2 ... clears variables v1, v2, and so on from the currently active workspace. Each input must be an unquoted string specifying the variable to be cleared. This string may include the wildcard character (*) to clear all variables that match a pattern. For example, clearvars X* clears all the variables in the current workspace that start with the letter X.
If any of the variables v1, v2, and so on, are global, clearvars removes these variables from the current workspace only, leaving them accessible to any functions that declare them as global.
clearvars -global removes all global variables, including those made global within functions.
clearvars -global v1 v2 ... completely removes the specified global variables.
The -global flag may be used with any of the following syntaxes. When used in this way, it must immediately follow the function name.
clearvars -regexp p1 p2 ... clears all variables that match regular expression patterns p1, p2, and so on.
clearvars -except v1 v2 ... clears all variables except for those specified following the -except flag. Use the wildcard character '*' in a variable name to exclude variables that match a pattern from being cleared. clearvars -except X* clears all the variables in the current workspace, except for those that start with X, for instance. Use clearvars -except to keep the variables you want and remove all others.
clearvars -except -regexp p1 p2 ... clears all variables except those that regular expression patterns p1, p2. If used in this way, the -regexp flag must immediately follow the -except flag.
clearvars v1 v2 ... -except -regexp p1 p2 ... can be used to specify variables to clear that do not match specified regular expression patterns.
clearvars -regexp p1 p2 ... -except v1 v2 ... clears variables that match p1, p2, ..., except for variables v1, v2, ...
Clear variables starting with a, except for the variable ab:
clearvars a* -except ab
Clear all global variables except those starting with x:
clearvars -global -except x*
Clear variables that start with b and are followed by 3 digits, for the variable b106:
clearvars -regexp ^b\d{3}$ -except b106Clear variables that start with a, except those ending with a:
clearvars a* -except -regexp a$
clear, exist, global, persistent, save, who, whos
MATLAB Workspace in the Desktop Tools and Development Environment documentation
![]() | clear | clear (serial) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |