MATLAB: How to detect if a figure exist

figureMATLABsaveas

To save the figure if one exists with:
saveas(gcf,figname);
the problem is that if no figure exists, it'll create a figure. How to detect if a figure exist before using above saveas command to avoid the creation of an empty figure?
Thanks.

Best Answer

In 2014b and later you can query whether the Graphics Root objects has any children:
g = groot;
isempty(g.Children) % True if there are no open graphics objects, false otherwise
Related Question