GUI, selecting a figure to close, if it exists?

1 view (last 30 days)
I have a GUI that sometime at the end displays a new figure (handles.hf5). I create the new figure using:
handles.hf5=figure('position',[xpos, ypos, sz(2), sz(1)]);
When I run my GUI again, if this figure exists, I want to be able to close it. (sometimes I run the GUI with other options and no additional figure is required and so handles.hf5 does not exist.
I have tried the following but none work:
close all
allPlots = findall(0, 'Type', 'figure', 'FileName', [])
delete(allPlots);
and
if ishandle(handles.hf5)
close(handles.hf5)
end

Accepted Answer

Jason
Jason on 18 Jan 2016
A reply on another question from Walter has resolved the issue.
figure1 is the tag of the main GUI.
fig1h = findall(0,'type','figure','Tag','figure1'); %Keep this open as its the main gUI figure
figh = findall(0,'type','figure');
other_figures = setdiff(figh, fig1h);
delete(other_figures)

More Answers (1)

Walter Roberson
Walter Roberson on 11 Jan 2016
If you are using R2014b or newer, use isgraphics()
In earlier releases, using ishandle() should work provided that the variable exists and has not been set to 0
if isfield(handles, 'hf5') && handles.hf5 ~= 0 && ishandle(handles.hf5)
delete(handles.hf5);
end
Note: close() can be intercepted by the figure's CloseRequestFcn callback, but delete() cannot be intercepted.
  4 Comments
Walter Roberson
Walter Roberson on 11 Jan 2016
You are using R2014b or later, so you should compare to groot instead of to 0, and you should perhaps check ~isempty() as well.
I have not gone through your code otherwise; it is after 5am here and I am going to head to bed.

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!