tags:

views:

2716

answers:

5

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?

A: 

To close figure there is the "close" function. I'm still looking one to check if a figure is open.

Andrea Ambu
+3  A: 

close all

will close all open figures.

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

figure('name','banana')

Creates a figure with the name banana

close(findobj('type','figure','name','orange'))

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

close(findobj('type','figure','name','banana'))

Closes the figure.

You can specify search parameters to meet your needs.

KennyMorton
cool, it works! Thank you!
Andrea Ambu
+3  A: 

I'm a little unclear about what you mean by "open". Figures don't really have "open" or "closed" states. They either exist or they don't. The FIGURE command will return a handle to the figure it makes:

hFig = figure(...your arguments here...);

You can also get a figure handle from the FINDOBJ function, which will find all graphics objects matching the property values you pass to it:

hFig = findobj(...your property/value pairs here...);

You can get rid of a figure with either of these commands:

close(hFig);
delete(hFig);

You can check if a figure has been closed/deleted using the function ISHANDLE:

ishandle(hFig)  % Returns 'true' if the figure exists, 'false' if it doesn't

Figures can also be "visible" or "invisible". They have a 'Visible' property that you can get or set the value of:

get(hFig,'Visible')        % Returns 'on' or 'off'
set(hFig,'Visible','off')  % Makes a figure invisible,
                           % but it still exists (i.e. it's not closed)

If you're wanting to check if a figure is minimized, that may be a little more difficult. I believe there are some files that may help you with that on the MathWorks File Exchange: here's one to check out.

gnovice
Interesting answer, +1. Anywaay I've done it with findobj. Nice to know these things!
Andrea Ambu
+3  A: 

In MATLAB, you can GET information on the 'root'. Figures are children of 'root' (handle of root is 0) they are the only children of the root.

http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f7-41259.html

Knowing this, you can try this code that looks for the children of root, and gives you a list.

>> close all
>> get(0,'children')
ans =
   Empty matrix: 0-by-1
>> figure(1)
>> get(0,'children')
ans =
     1
>> figure(3)
>> get(0,'children')
ans =
     3
     1

I think you will find this the most direct way to query what figures are open.

MatlabDoug
+1  A: 
isempty(findobj('name','Your_Figure_Name'))

if the answer is 0, then your figure is open

Latif Yalcinoglu

related questions