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

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

 Akzeptierte Antwort

Jason
Jason am 18 Jan. 2016

0 Stimmen

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)

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 11 Jan. 2016

0 Stimmen

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 Kommentare

Thankyou Walter, but its still keeping the figure open.
This is what I have done to try and investigate. In my pushbutton callback I have:
handles.hf5=figure('position',[xpos, ypos, sz(2), sz(1)]);
guidata(hObject, handles);
allPlots2 = findall(0, 'Type', 'figure', 'FileName', [])
and teh result of allplots is:
allPlots2 =
0x0 empty GraphicsPlaceholder array.
A bit more info. Here is the begining of my pushbutton call back:
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%close all;
allPlots = findall(0, 'Type', 'figure', 'FileName', [])
% delete(allPlots);
tf4 = isgraphics(handles.hf4)
tf5 = isgraphics(handles.hf5)
if isfield(handles, 'hf5') && handles.hf5 ~= 0 && ishandle(handles.hf5)
delete(handles.hf5);
end
if isfield(handles, 'hf4') && handles.hf4 ~= 0 && ishandle(handles.hf4)
delete(handles.hf4);
end
[sigma, fwhm]=xObjSize(hObject,eventdata,handles,IM);
%I then create another figure hf5.
handles.hf5=figure('position',[xpos, ypos, sz(2), sz(1)]);
guidata(hObject, handles);
I use tf4 and tf5 to show me the state of the handles.hf4 and handles.hf5 objects which are supposed to be my figures hf4 & hf5.
Later on in this callback I call a function xobjeSize which inside creates a figure hf4. Heres the main part.
function [sigma, fwhm] = xObjSize(hObject,eventdata,handles,IM)
% do some stuff and then create a figure
%Create Figure----------------------------------------------------------
fontSize=8
scrsz = get(groot,'ScreenSize');
sz = [400 1400]; % figure size
screensize = get(0,'ScreenSize');
xpos = ceil((screensize(3)-sz(2))/2); % center the figure on the
ypos = ceil((screensize(4)-sz(1))/2); % center the figure on the
handles.hf4=figure('position',[xpos, ypos, sz(2), sz(1)]);
guidata(hObject,handles);
tf4=isgraphics(handles.hf4) %check status
%---------------------------------------------------------------------
The first time I run it, both figures are created and then persist. The 2nd time I run it, when I check to see the status of the figures hf4 and hf5 using the code sat the top of the callback
tf4 = isgraphics(handles.hf4)
tf5 = isgraphics(handles.hf5)
i get an error for tf4
Reference to non-existent field 'hf4'.
hf5 works fine now and does indeed delete its figure so there is no accumulation. The only problem is the figure hf4.
Walter Roberson
Walter Roberson am 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.
Jason
Jason am 12 Jan. 2016
Yes I am using 2014b.

Melden Sie sich an, um zu kommentieren.

Kategorien

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

Gefragt:

am 11 Jan. 2016

Beantwortet:

am 18 Jan. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by