19 Downloads
Updated 10 Aug 1999
No License
Editor's Note: This file was a File Exchange Pick of the Week.
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.
David Yang (2021). keep (https://www.mathworks.com/matlabcentral/fileexchange/181-keep), MATLAB Central File Exchange. Retrieved .
Inspired: keep2, KEEP3, keepfield, Keep4, Clear All But
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
I second Ray Ducharme's comment in that you can use clearvars -except var1 var2 ... and not have to carry around a third party function
Works awesome for my player many 10x !!
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.
Very useful function and simpler than regular expressions.
Just magic,
Thx :)
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!
Really handy little script. Saves lots of time as well as minimising size of saved workspaces. Many thanks.
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
thanks
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])
Thank you! this is great.
Bravissimo!
Brilliant! This is incredibly useful.
Excellent tool! MATLAB should include this.
Perfect code, Thanks!
Does Mathworks need to pay a lot to make this function in the built-in lib?
I love having this script. Thank you for posting!
Excellent
A very helpful piece of code, just what I needed. Well done to the author!
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!
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.
Why on earth is this function not part of MATLAB to begin with??
Muchas gracias por tu trabajo, good job.
This is gold
This file saves lots of time and extra coding.
Excellent little function!!!
Super - saves lines and lines of clean codes...
I agree this should come with MATLAB.
Very nice for cleaning up memory while holding onto important results
It is a very good and practical tool.
Exactement ce que je cherchais.
Perfect! why is this not a standard matlab function yet?
Good job!
yes, that's exactly what I am looking for.
Thanks!
Just wonderful. Works like a charm. Thanks a lot.
You are great!!
Thanks a lot, you have saved much of my time!!
great !!
Thanks a lot excellent work
Smart function indeed! Good thing for us that you thought about it!
thanks
Superb! I think it is obvious there should be a reciprocal to the clear function like this in matlab's inherent set of functions.
Nifty function. Exactly what I was searching for.
Thanks !
Cool function - simple and useful