Code covered by the BSD License  

Highlights from
clearnot

clearnot

by

 

CLEARNOT clears all variables except the input variables

clearnot(varargin)
function clearnot(varargin)
%CLEARNOT clears all variables except the input variables
%   CLEARNOT A B clears all the variables in the calling workspace except
%   A and B (or any variable that is input to CLEARNOT).
%
%   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
%
%   Copyright Andy Bliss May 23, 2006

%get variables from caller workspace
variablestoclear=evalin('caller','who');
%remove the input arguments from that list
[C,IA,IB]=intersect(variablestoclear,varargin);
notclearindex=true(length(variablestoclear),1);
notclearindex(IA)=false;
variablestoclear=variablestoclear(notclearindex);
%we want to clear the variablestoclear variable too
variablestoclear{end+1}='variablestoclear';
%we have to assign variablestoclear to the caller workspace so the clear 
%command works in the caller workspace
assignin('caller','variablestoclear',variablestoclear);
%And finally, clear the variables we don't want
evalin('caller','clear(variablestoclear{:})')

Contact us