[ACCEPTED]-How to check if a figure is opened and how to close it?-matlab

Accepted answer
Score: 18
close all

Will close all open figures.

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

figure('name','banana')

Creates a 4 figure with the name banana.

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

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

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

Closes the figure.

You can specify 1 search parameters to meet your needs.

Score: 14

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

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

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

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

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

close(hFig);
delete(hFig);

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

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

Figures 7 can also be "visible" or "invisible". They 6 have a 'Visible' property that you can get or set the value 5 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 4 is minimized, that may be a little more 3 difficult. I believe there are some files 2 that may help you with that on the MathWorks 1 File Exchange: here's one to check out.

Score: 4

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

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

Knowing this, you can try this 4 code that looks for the children of root, and 3 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 2 the most direct way to query what figures 1 are open.

Score: 2
isempty(findobj('name','Your_Figure_Name'))

if the answer is 0, then your figure is 1 open

Score: 1

If inside your method, you create a figure 7 without a 'name':

function [] = myMethod()
    myFigure = figure()
end

you won't be able to access 6 myFigure handle the next time through. So:

function [] = myMethod()
    if ishandle(myFigure) % will fault, cant find variable myFigure
        close(myFigure)  % will fault
        delete(myFigure) % will fault
    end

    myFigure = figure()
end

gnvoice 5 wasn't 100% clear when he says:

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

He means you can 2 only check AFTER you have recovered the 1 handle:

function [] = createMyFigure()
    recoveredHandle = findobj('type','figure', 'Name', 'myFigureName')
    close(recoveredHandle)
    delete(recoveredHandle)
    ishandle(recoveredHandle)

    myFigure = figure('Name','myFigureName') % now create figure
end
Score: 0

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

More Related questions