Noclear.m

From Pnb
Jump to navigation Jump to search
%NOCLEAR.M
% script to clear all variables but those listed in noclear_list, a cell
% array of variable names. Include noclear_list if you wish to keep it.
% If noclear_list is absent, performs a clear.
%
% useage: noclear

function noclear(varargin)
if isempty(varargin),
  dum=evalin('base','who');
  for i=1:size(dum,1),
    if findstr('noclear',dum{i}),
      evalin('base','noclear(noclear_list)');
      return
    end
  end
  evalin('base','clear')
  return
else
  dum=[];
  for i=1:size(varargin,2)
    if ischar(varargin{i}),
      dum=strvcat(dum,varargin{i});
    elseif iscell(varargin{i})
      dum=strvcat(dum,char(varargin{i}));
    else
      error('input needs to a string, an array or cell array of strings')
    end
  end
  noclear_list=cellstr(dum);  
end
s_clear=evalin('base','who');
s_clear0=s_clear;
for i_clear=1:size(s_clear,1),
  i_clear;
  a_clear=1;
  for j_clear=1:size(noclear_list,1),
    j_clear;
    if size(s_clear{i_clear},2)==size(noclear_list{j_clear},2),
      if all(s_clear{i_clear}==noclear_list{j_clear}),
	a_clear=0;
	break
      end 
    end
  end 
  doit=['clear ' s_clear{i_clear}];
  if a_clear,
    evalin('base',doit)
  end
  s_clear=s_clear0;
end
clear a_clear i_clear j_clear s_clear s_clear0

noclear