File Exchange

image thumbnail


version 1.0 (1.83 KB) by

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

49 Ratings



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.

Comments and Ratings (54)

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

David Cooper

Chad Greene

Chad Greene (view profile)

Works awesome for my player many 10x !!


Karl (view profile)

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.


Karl (view profile)



Brady (view profile)

Grzegorz Knor

Very useful function and simpler than regular expressions.


Just magic,

Thx :)

Ray Ducharme

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)]);

 keep 'x y'

Thanks David for the original function!

Tina Hegarty

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


shah (view profile)



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

Kristyn Falkenstern

Thank you! this is great.

Touko Pouko



Brilliant! This is incredibly useful.

Sushil Subramanian

Excellent tool! MATLAB should include this.

Matthew Rydzik

Perfect code, Thanks!


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

Chris Andrews

I love having this script. Thank you for posting!

Cornelis P. Bogerd


Mark Thommy

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

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!

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.


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

Mauricio Fernandez

Muchas gracias por tu trabajo, good job.

Andrew Gee

This is gold

Andre Crawford

This file saves lots of time and extra coding.

Taon Khuu

Excellent little function!!!

Christoph Ullrich

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

Daniel Vasques

I agree this should come with MATLAB.

Andrew Lingg

Very nice for cleaning up memory while holding onto important results

Kristofer Svensson

Benjamin Fenech

Ivan Ordonez

It is a very good and practical tool.

Jerome G.

Exactement ce que je cherchais.

D. Gietelink

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

Yi Li

Good job!

Hui Shi

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

Camilo Cano

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

Athanasios Pechlivanis

You are great!!

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

joep van berkel

great !!

Ilker Delice

Thanks a lot excellent work

Yaser Ahmed

Fahad Mahmood

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

B. Peña


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.

Till Bockemuehl

Nifty function. Exactly what I was searching for.

Matt Remigy

Thanks !

Suresh Joel

Cool function - simple and useful

MATLAB Release
MATLAB 5.2 (R10)

Inspired: keep2, KEEP3, keepfield, Keep4, Clear All But

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

» Watch video