Matlab – How to check if a figure is opened and how to close it


My m-file opens figures depending on parameters. Sometimes is one figure, sometimes it opens 2 figures.

If the user call the function, the figures appear. If he calls the function again, with other parameters, I'm clearing figures with clf before the new plots.

If the second call is set to draw only one figure, the second one (opened by the previous call) remain gray (because of the clf).

Is there any way to check if it is opened and close it?

Best Answer

close all

Will close all open figures.

You can use findobj() to find objects that may exist by specifying search parameters. For example:


Creates a figure with the name banana.


Does nothing because there are no figures open with the name orange.


Closes the figure.

You can specify search parameters to meet your needs.

Related Topic