Close command and Invalid figure handle

I have a GUI in which I have a button create a plot: p(1)=plot(...).
I have a reset button that, upon being pressed, executes close(p).
When I hit the reset button, I get the error:
??? Error using ==> close at 94
Invalid figure handle.
Error in ==> Pulse>reset_Callback at 870
close(p)
However, while the GUI and p(1) plot figure are open, if I type close(p) in the Matlab Command Window, it closes the figure without any errors. Any idea what's causing this?

 Akzeptierte Antwort

Jan
Jan am 29 Jan. 2013

2 Stimmen

Or:
close(ancestor(p, 'figure'))

1 Kommentar

Titus Edelhofer
Titus Edelhofer am 29 Jan. 2013
Interesting, another function that I did not know it exists ;-).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Titus Edelhofer
Titus Edelhofer am 29 Jan. 2013

1 Stimme

Hi Jared,
you need to close the figure, not the plot. Did you write the GUI using GUIDE? In this case you could write
close(handles.figure1)
assuming you did not assign another tag to the figure ...
Slightly less "nice": just close the figure by
close(gcbf);
where gcbf stands for "get callback figure".
Titus

2 Kommentare

Jared
Jared am 29 Jan. 2013
I phrased my question wrong. p is indeed a figure, with a plot in it. So I may have p(1)...p(5) figures holding plots, and h(1)...h(5) holding histograms. I was just trying to close all of p or all of h.
Titus Edelhofer
Titus Edelhofer am 30 Jan. 2013
Hi Jared,
close(p) should work. If they are all figures, all figures should close. There is a problem of course if at least one entry of p is not a figure (because it has been closed e.g. by the user). What you can do is test this during the call by writing close(p(ishghandle(p)))
Titus

Melden Sie sich an, um zu kommentieren.

Azzi Abdelmalek
Azzi Abdelmalek am 28 Jan. 2013

0 Stimmen

Maybe you are trying to close a figure that is already closed. Use
close(p(1))

3 Kommentare

Jared
Jared am 28 Jan. 2013
Bearbeitet: Jared am 28 Jan. 2013
Shouldn't close(p) close any of them...p(1), p(2), p(3), etc, that exist? How can I close them in a single command without checking to see which are open? Using close p(1) does indeed work, but I don't see why close(p) doesn't, especially since it does work from the command window.
It does not work from command window. Check this
p(1)=figure
p(2)=figure
close(p(1)) % will close figure1
close(p) % error
How p is stored? when you close a figure, you have to update p:
p=handles.p
close(p(2))
p(2)=[]
handles.p=p

Melden Sie sich an, um zu kommentieren.

Shashank Prasanna
Shashank Prasanna am 29 Jan. 2013

0 Stimmen

I don't recommend this at all, but depending on your use-case they may be useful with modifications:
close(findobj('type','figure'))
Closes every figure. Also as Titus mentioned, there is no such thing as a plot object, there are figures, axes, line objects and so on. Plot returns column vector of handles to lineseries objects.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 28 Jan. 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by