How to detect if a figure exist?
88 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
Brendan Hamm
2016 年 1 月 14 日
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
2 件のコメント
Roger Breton
2022 年 2 月 8 日
Thank you for sharing this tidbit of code!
It worked perfect for me in my script.
But I could not find "CurrentFigure' in the list of properties when I issued get(fig) from the command window in debuig mode?
その他の回答 (1 件)
Stephen23
2016 年 1 月 14 日
編集済み: Stephen23
2016 年 1 月 14 日
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.
ishghandle(stored_handle)
2 件のコメント
Stanislav Ginzburg
2022 年 11 月 13 日
Sometimes you may get handle on empty object plot([1,2], [NaN, NaN]); Result of ishghandle is true, Groot return handler of figure. And not empty too. The good practice is to prevent save empty objects.
Stephen23
2022 年 11 月 13 日
"The good practice is to prevent save empty objects."
No, empty plots may also be information: it may be a perfectly valid and important result that for some figure nothing is plotted. This depends entirely on the specific situation and thus is the responsibility of the user to check.
参考
カテゴリ
Help Center および File Exchange で Printing and Saving についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!