View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.8 | 6 ratings Rate this file 7 Downloads (last 30 days) File Size: 2.33 KB File ID: #7129 Version: 1.0




12 Mar 2005 (Updated )

Keeps the listed variables in the calling workspace with a command format similar to "clear"

| Watch this File

File Information

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


Keep, Keep2, and Keep3 inspired this file.

MATLAB release MATLAB 7.2 (R2006a)
Other requirements None
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
16 Aug 2016 Erik

Erik (view profile)

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

19 Aug 2013 Marianne

Thank you very much, exactly what i needed

21 Jan 2013 Inkeun Seo


05 May 2009 Martin

Martin (view profile)

29 Aug 2006 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.

Comment only
08 Jun 2006 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)

01 Apr 2006 Bo Gao

very nice job!!

27 Jun 2006

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

28 Jun 2006

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

Contact us