No BSD License  

Highlights from
delobject

from delobject by Almar Klein
Similar to DELETE, but more forgiving, and allows deletion of multiple objects.

delobject(h)
function delobject(h)
% DELETE OBJECT
%
% DELOBJECT( H )  deletes the object with handle h, but is more forgiving
% than DELETE: when h is empty, zero or not valid, no action is taken.
%
% Additionally, DELOBJECT enables deletion of multiple handles at the same
% time:
% - H can be an array of handles; all objects are deleted. 
% - H can be a cell array of handles.
% - H can be a struct, in which case each field is assumed a handle.
% - Each element in the cell array and each field in the struct can be an
%   array of handles. Note that using cells and structs is slower than a
%   numeric array of handles.
%
%               Example:
% h  = 0;
% hf = figure; hold on; 
% axis([-15 15 -15 15]); axis equal;
% for i=1:10;   
%   x = sin(0:0.1:2*pi);
%   y = cos(0:0.1:2*pi);
%   delobject(h);
%   h.red   = plot( x,y+i,'r');
%   h.blue  = plot( x,y-i,'g');
%   pause(0.5);
% end
% delobject(hf);
%
% See also delete


%% CHECK INPUT

if nargin~=1;  error([mfilename ': need exactly one input argument!']);  end;


%% HANDLE is NUMERIC or CELL or STRUCT

if isnumeric(h)
    delarray(h);
    
elseif iscell(h)
    for i=1:length(h)
       delarray( h{i} ); 
    end
    
elseif isstruct(h)
    fnames = fieldnames(h);    
    for i=1:length(fnames)
       delarray( h.(fnames{i}) ); 
    end
    
else
    error([mfilename ': input must be NUMERIC or CELL or STRUCT!']);
end


%% SUBFUNCTION TO DELETE AN ARRAY
function delarray(hh)
if ~isnumeric(hh);  error([mfilename ': inproper input, read the help!']);  end;
for i=1:length(hh)
   del(hh(i)); 
end


%% SUBFUNCTION TO DELETE A SINGLE HANDLE
function del(h)
if ~isempty(h) && h~=0 && ishandle(h)
   delete(h); 
end

Contact us at files@mathworks.com