How to detect if a figure exist?
125 views (last 30 days)
To save the figure if one exists with:
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?
Brendan Hamm on 14 Jan 2016
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
More Answers (1)
Stephen23 on 14 Jan 2016
Edited: Stephen23 on 14 Jan 2016
The most reliable solution is to move your programming up a notch: use explicit handles and keep track of them.
While gcf and gca are fine for the command-line and playing with scripts, when you want to write more advanced functions then:
- do not use gcf, gca.
- store the handle of every axes, figure, and other relevant objects.
- explicitly specify the parent of every object when it is created, plotted, etc.
When you code properly then your quest is trivial using ishghandle: