No BSD License  

4.95745

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

keep

by

 

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 file inspired Clear All But, Keep3, Keep2, Keep4, and Keepfield.

MATLAB release MATLAB 5.2 (R10)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (52)
17 Oct 2012 Chad Greene  
21 Apr 2012 Deyan Dobromirov

Works awesome for my player many 10x !!

18 Mar 2012 Karl

A useful edit would be to check for the workspace for the variables and error out if not found. Otherwise if you mistype, you lose the variable you want to keep.

18 Mar 2012 Karl  
28 Jun 2011 Michael  
08 Dec 2010 Brady  
30 Aug 2010 Grzegorz Knor

Very useful function and simpler than regular expressions.

21 Apr 2010 Airballman

Just magic,

Thx :)

24 Feb 2010 Ray Ducharme  
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!

17 Feb 2010 Tina Hegarty

Really handy little script. Saves lots of time as well as minimising size of saved workspaces. Many 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

30 Sep 2009 shah

thanks

13 Aug 2009 Florian  
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])

08 Oct 2008 Kristyn Falkenstern

Thank you! this is great.

23 Sep 2008 Touko Pouko

Bravissimo!

18 Sep 2008 K R

Brilliant! This is incredibly useful.

12 Jul 2008 Sushil Subramanian

Excellent tool! MATLAB should include this.

08 Jul 2008 Matthew Rydzik

Perfect code, Thanks!

14 May 2008 H D

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

04 May 2008 Chris Andrews

I love having this script. Thank you for posting!

09 Apr 2008 Cornelis P. Bogerd

Excellent

28 Mar 2008 Mark Thommy

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

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

25 Feb 2008 J J

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

19 Nov 2007 Mauricio Fernandez

Muchas gracias por tu trabajo, good job.

02 Oct 2007 Andrew Gee

This is gold

27 Sep 2007 Andre Crawford

This file saves lots of time and extra coding.

16 Aug 2007 Taon Khuu

Excellent little function!!!

24 May 2007 Christoph Ullrich

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

11 Apr 2007 Daniel Vasques

I agree this should come with MATLAB.

29 Mar 2007 Andrew Lingg

Very nice for cleaning up memory while holding onto important results

16 Nov 2006 Kristofer Svensson  
05 Jun 2006 Benjamin Fenech  
18 May 2006 Ivan Ordonez

It is a very good and practical tool.

03 Jan 2006 Jerome G.

Exactement ce que je cherchais.

01 Aug 2005 D. Gietelink

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

29 Jun 2005 Yi Li

Good job!

10 Jun 2005 Hui Shi

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

02 May 2005 Camilo Cano

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

17 Feb 2005 Athanasios Pechlivanis

You are great!!

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

03 Jan 2005 joep van berkel

great !!

16 Sep 2004 Ilker Delice

Thanks a lot excellent work

03 Jun 2004 Yaser Ahmed  
19 Feb 2004 Fahad Mahmood

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

07 Oct 2003 B. Peña

thanks

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.

26 Jun 2003 Till Bockemuehl

Nifty function. Exactly what I was searching for.

14 Jun 2002 Matt Remigy

Thanks !

28 May 2002 Suresh Joel

Cool function - simple and useful

Contact us