File Exchange

image thumbnail

clearnot

version 1.0.0.0 (1.43 KB) by Andrew Bliss
CLEARNOT clears all variables except the input variables

1 Download

Updated 09 Jan 2013

View License

CLEARNOT A B clears all the variables in the calling workspace except A and B (or any variable that is input to CLEARNOT).

Useful for cleaning up a cluttered workspace when you want to preserve a few variables.

The functional form of CLEARNOT is also valid: CLEARNOT('A','B').

TODO: make it work with wildcards.

Example:
a=2;b=3;c=4;d=5;
clearnot a c
This clears B and D but preserves A and C in the workspace.

Cite As

Andrew Bliss (2021). clearnot (https://www.mathworks.com/matlabcentral/fileexchange/39776-clearnot), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)

Chad Greene

Oliver: I have used the keep function for years, but I have always thought it was poorly named. Matlab will always keep your variables unless you tell it to do something else with them. It took me years to find the keep function on FEX because I never searched for the word 'keep'. I still have the darndest time remembering the function name because my thought process is typically that I want to clear all variable except a few. In other words, clearing comes to mind first, and then I think about keeping. This function is intuitively named and can be found easily using Function Hints. That is, I can type "clear" and tab to find "clearnot".

Oliver Woodford

Have you seen the submissions keep, keep2, keep3, keep4, keep5? They all do the same thing as this. In what way is this submission an improvement?

Jan

The built-in function Richard has mentioned should be preferred.

In addition the method can be simplified:
vars = setdiff(evalin('caller','who'), varargin);
evalin('caller', ['clear ', sprintf('%s ', vars{:})]);

Richard Crozier

I should also point out clearvars already works with wildcards, and also regular expressions.

Richard Crozier

how about the built-in function clearvars, with the -except flag. Your example would then be:

clearvars -except a c

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!