No BSD License  

Highlights from
keep

4.95455

5.0 | 44 ratings Rate this file 72 Downloads (last 30 days) File Size: 1.83 KB File ID: #181

keep

by David Yang

 

10 Aug 1999

KEEP is complementary to the "clear" command: it keeps the supplied variables and deletes the rest.

Editor's Notes:

This file was a File Exchange Pick of the Week.

| Watch this File

File Information
Description

KEEP is complementary to the "clear" command: instead of clearing the supplied variables, it keeps them and deletes the rest.

For example, the command " keep x y " clears all variables from the workspace except x and y.

Acknowledgements
This submission has inspired the following:
KEEP3, keep2, Keep4, keepfield
MATLAB release MATLAB 5.2 (R10)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (48)
28 May 2002 Suresh Joel

Cool function - simple and useful

14 Jun 2002 Matt Remigy

Thanks !

26 Jun 2003 Till Bockemuehl

Nifty function. Exactly what I was searching for.

25 Sep 2003 Rasmus Anthin

Superb! I think it is obvious there should be a reciprocal to the clear function like this in matlab's inherent set of functions.

07 Oct 2003 B. Peña

thanks

19 Feb 2004 Fahad Mahmood

Smart function indeed! Good thing for us that you thought about it!

03 Jun 2004 Yaser Ahmed  
16 Sep 2004 Ilker Delice

Thanks a lot excellent work

03 Jan 2005 joep van berkel

great !!

17 Feb 2005 Athanasios Pechlivanis

You are great!!

Thanks a lot, you have saved much of my time!!

02 May 2005 Camilo Cano

Just wonderful. Works like a charm. Thanks a lot.

10 Jun 2005 Hui Shi

yes, that's exactly what I am looking for.
Thanks!

29 Jun 2005 Yi Li

Good job!

01 Aug 2005 D. Gietelink

Perfect! why is this not a standard matlab function yet?

03 Jan 2006 Jerome G.

Exactement ce que je cherchais.

18 May 2006 Ivan Ordonez

It is a very good and practical tool.

05 Jun 2006 Benjamin Fenech  
16 Nov 2006 Kristofer Svensson  
29 Mar 2007 Andrew Lingg

Very nice for cleaning up memory while holding onto important results

11 Apr 2007 Daniel Vasques

I agree this should come with MATLAB.

24 May 2007 Christoph Ullrich

Super - saves lines and lines of clean codes...

16 Aug 2007 Taon Khuu

Excellent little function!!!

27 Sep 2007 Andre Crawford

This file saves lots of time and extra coding.

02 Oct 2007 Andrew Gee

This is gold

19 Nov 2007 Mauricio Fernandez

Muchas gracias por tu trabajo, good job.

25 Feb 2008 J J

Why on earth is this function not part of MATLAB to begin with??

28 Feb 2008 Sulabh Dhanuka

Though simple in idea, it is such a useful function. I have wished for something like this for so long. I agree, it makes no sense for this to be a part of Matlab.

20 Mar 2008 M M Dupin

Dear MATLAB developers, please listen to users and add this *extremely* useful function in the core of MATLAB. Plus, it has already been tested by us!

28 Mar 2008 Mark Thommy

A very helpful piece of code, just what I needed. Well done to the author!

09 Apr 2008 Cornelis P. Bogerd

Excellent

04 May 2008 Chris Andrews

I love having this script. Thank you for posting!

14 May 2008 H D

Does Mathworks need to pay a lot to make this function in the built-in lib?

08 Jul 2008 Matthew Rydzik

Perfect code, Thanks!

12 Jul 2008 Sushil Subramanian

Excellent tool! MATLAB should include this.

18 Sep 2008 K R

Brilliant! This is incredibly useful.

23 Sep 2008 Touko Pouko

Bravissimo!

08 Oct 2008 Kristyn Falkenstern

Thank you! this is great.

25 Jun 2009 Thomas Deneux

looks great, but the code can be much more simple!
for example,

function keep(varargin)

% variables to clear in a cell array
varlist = evalin('caller','who');
varlist = setdiff(varlist,varargin);

% make a string (separate the variable by a space)
[varlist{:,2}] = deal(' ');
varlist = varlist';
varstr = [varlist{:}];

% and clear!
evalin('caller',['clear ' varstr])

13 Aug 2009 Florian  
30 Sep 2009 shah

thanks

30 Jan 2010 Thorsten

I used the code by Thomas Deneux, which was really helpful and instructive. However, maybe due to an old MATLAB version, it didn't work for me with many variables to be cleaned.

Modifying the middle block like this worked fine for my, although it's a rather crude fix...

% make a string (separate the variable by a space)
% [varlist{:,2}] = deal(' '); modified
varlist=strcat(varlist, '?');
varlist = varlist';
% varstr = [varlist{:}];
varstr=regexprep([varlist{:}],'?',' '); % modified

17 Feb 2010 Tina Hegarty

Really handy little script. Saves lots of time as well as minimising size of saved workspaces. Many thanks.

24 Feb 2010 Ray Ducharme

FYI, users with newer version can now use "clearvars -except". To 'keep' old habits...

 function keep(varargin)
    evalin('caller',['clearvars -except ' char(varargin)]);
 end

call...
 keep 'x y'

Thanks David for the original function!

24 Feb 2010 Ray Ducharme  
21 Apr 2010 Airballman

Just magic,

Thx :)

30 Aug 2010 Grzegorz Knor

Very useful function and simpler than regular expressions.

08 Dec 2010 Brady  
28 Jun 2011 Michael  
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
clear variables David Yang 22 Oct 2008 06:33:29
tools David Yang 22 Oct 2008 06:33:29
keep complementary David Yang 22 Oct 2008 06:33:29
utilities David Yang 22 Oct 2008 06:33:29
clear variables Luis 08 Jul 2010 22:15:30
clear variables Goran Rovišan 11 Jan 2012 08:18:53

Contact us at files@mathworks.com