MATLAB: How to delete a graphics object when clearing the object handle variable

cleardeletegraphicshandleobject

E.g.
f = figure;
clear f;
How can I programtically make it so that the figure that is created will be closed when the second line of code is run?
I.e., I want clearing the graphics handle to also delete the graphics object.

Best Answer

Here is a one-line solution, but you have to pass an actual named variable for it to work properly,
>> delclear(f) %one line
function delclear(f)
delete(f);
name=inputname(1);
if ~isempty(name)
evalin('caller', ['clear ',inputname(1)]) ;
else
warning 'Deleted, but not cleared.'
end
end