File Exchange

image thumbnail


version (2.33 KB) by Mirko Hrovat
Keeps the listed variables in the calling workspace with a command format similar to "clear"


Updated 28 Aug 2006

View License

This is another version of Keep, which allows a user to specify what variables are kept in the workspace of the calling routine. "Keep" compliments "Clear".

This version has been extended to match the command format of the "clear" command for clearing variables. Thus wildcards and regular expressions are allowed. The version also supports the "global" key word and Keep will not touch global variables unless the "global" key word is used.

Examples of supported formats:
keep var1 var2
keep var*
keep -regexp expr1
keep global
keep global var1
keep global -regexp expr1

Comments and Ratings (7)


Possibly the most useful function on the file exchange. This is IMHO the best keep variant on here. (I've considered keep and keep2 through keep5).


Thank you very much, exactly what i needed

Inkeun Seo



Mirko Hrovat

Thanks to David for pointing out the bug when there are no variables to delete. This generated an unnecessary error message. Comments were also improved to delineate the examples more clearly.

David the Dude

Useful function but there is a bug:

The function returns an error if only one workspace variable exists. To reproduce. Clear workspace initialize 1 variable X and run keep(X)

Bo Gao

very nice job!!


Updated comments to improve code metrics.

Another bug fix which fixes the same issue as the prior bug fix but with global variables.

Bug fix to avoid error message when argument list matches the entire workspace variable set. For this case there are no variables to be "cleared".

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: keep, keep2, KEEP3

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor