keep

PURPOSE ^

KEEP keeps the caller workspace variables of your choice and clear the rest.

SYNOPSIS ^

function keep(varargin);

DESCRIPTION ^

KEEP keeps the caller workspace variables of your choice and clear the rest.
       Its usage is just like "clear" but only for variables.

       Xiaoning (David) Yang   xyang@lanl.gov 1998
       Revision based on comments from Michael McPartland,
       michael@gaitalf.mgh.harvard.edu, 1999

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function keep(varargin);
0002 %KEEP keeps the caller workspace variables of your choice and clear the rest.
0003 %       Its usage is just like "clear" but only for variables.
0004 %
0005 %       Xiaoning (David) Yang   xyang@lanl.gov 1998
0006 %       Revision based on comments from Michael McPartland,
0007 %       michael@gaitalf.mgh.harvard.edu, 1999
0008  
0009 %       Keep all
0010 if isempty(varargin)
0011         return
0012 end
0013 
0014 
0015 %       See what are in caller workspace
0016 wh = evalin('caller','who');
0017 
0018 
0019 %       Check workspace variables
0020 if isempty(wh)
0021         error('  There is nothing to keep!')
0022 end
0023 
0024 
0025 %       Construct a string containing workspace variables delimited by ":"
0026 variable = [];
0027 for i = 1:length(wh)
0028         variable = [variable,':',wh{i}];
0029 end
0030 variable = [variable,':'];
0031 
0032 
0033 %       Extract desired variables from string
0034 flag = 0;
0035 for i = 1:length(varargin)
0036         I = findstr(variable,[':',varargin{i},':']);
0037         if isempty(I)
0038                 disp(['       ',varargin{i}, ' does not exist!'])
0039                 flag = 1;
0040         elseif I == 1
0041                 variable = variable(1+length(varargin{i})+1:length(variable));
0042         elseif I+length(varargin{i})+1 == length(variable)
0043                 variable = variable(1:I);
0044         else
0045                 variable = [variable(1:I),variable(I+length(varargin{i})+2:length(variable))];
0046         end
0047 end
0048 
0049 
0050 %       No delete if some input variables do not exist
0051 if flag == 1
0052         disp('       No variables are deleted!')
0053         return
0054 end
0055 
0056 
0057 %       Convert string back to cell and delete the rest
0058 I = findstr(variable,':');
0059 if length(I) ~= 1
0060         for i = 1:length(I)-1
0061                 if i ~= length(I)-1
0062                         del(i) = {[variable(I(i)+1:I(i+1)-1),' ']};
0063                 else
0064                         del(i) = {variable(I(i)+1:length(variable)-1)};
0065                 end
0066         end
0067         evalin('caller',['clear ',del{:}])
0068 end

Generated on Thu 21-Aug-2014 10:40:31 by m2html © 2005