function keep(varargin) % ------------------------------------------------------------------------ % This function keeps variables specified and deteletes the rest of % variables in your workspace. % % For example, if you have x, y, and z in your workspace, % "keep x" clears y and z, and x will remain in the workspace. % ------------------------------------------------------------------------ % 05/14/06 ver. 1.0 % % Written by Munechika Katayama % ------------------------------------------------------------------------ % -- If there are no input arguments, does nothing -- if isempty(varargin) return end % -- Find all variables in the caller workspace -- allvar = evalin('caller','who'); % -- Determine variables to be erased -- clearvar = setdiff(allvar, varargin); % -- Check whether the input variables are in the workspace -- varin = intersect(allvar, varargin); varout = setdiff(varargin, varin); if isempty(varout) == 0 warning('Variable "%s" does not exist in the workspace', char(varout)) end % -- Create a string of the list of variables -- clearvar = [char(clearvar) char(kron(ones(length(clearvar),1), ' '))]; [clearvar errmsg] = sprintf('%s', clearvar'); % -- Delete the rest of the variables -- evalin('caller',['clear ',clearvar])