Reputation: 3488
I can close my main gui with a menu or the x button
function menuProgramQuit_Callback(hObject, eventdata, handles)
close(handles.figure1);
However at the same time a sub gui dialog might be open
function pushbuttonCalibrationConfigure_Callback(hObject, eventdata, handles)
calibrationOutput = uiConfigureCalibration('uiMain', handles.figure1);
waitfor(calibrationOutput);
guidata(handles.output,handles);
If the main figure was close the sub gui is not and further it crashes when he subgui (here
uiConfigureCalibration
) is closed, because the figure handle of the main gui is invalid.
So how can I close all sub guis in matlab before I close the main gui ?
EDIT: I changed the code such that the called dialog saves its handle to the handles of the main dialog
function uiConfigureCalibration_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for uiConfigureCalibration
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% save handle of calling gui
mainGuiHandleIndex = find(strcmp(varargin, 'uiMain'));
if ~isempty(mainGuiHandleIndex)
handles.mainHandle = varargin{mainGuiHandleIndex+1};
handlesMain = guidata(handles.mainHandle);
handlesMain.('openfigures').('calibration') = handles.figure1;
guidata(handles.mainHandle, handlesMain);
guidata(handles.figure1, handles);
end
In the closing function in the main gui I can no check if the figure us open and close it before the main figure is closed
if isfieldRecursive(handles, 'handles.openfigures.calibration')
close(handles.openfigures.calibration);
end
close(handles.figure1);
That however changes nothing. The sub-dialog figure is only closed AFTER the main dialog closes and the whole code crashes again.
Why is the figure of the sub dialog NOT closed with the close command?
Upvotes: 1
Views: 4052
Reputation: 2802
I had a similar problem where my gui would spawn other guis, namely an option gui and a log gui. In my main app I had things like this.
logFH = CreateLog;
optFH = CreateOptions;
These functions would create figures and return the handles. The handles can be saved in gui handles or with setappdata
and getappdata
. Later when I would close the main function.
if (isgraphics(logFH))
close(logFH);
delete(logFH);
end
Upvotes: 0
Reputation: 123
I just had the same problem, I wanted to close all the existing GUI windows when opening the main window.
I write an .m
function:
function closeAll()
clear all, close all, fclose('all'), clc, warning('off','MATLAB:xlswrite:AddSheet'); % close all opened items and clear Matlab workspace
end
and call this function in the GUI OpeningFcn or anywhere it is needed.
Upvotes: 0
Reputation: 20319
You can do this with the CloseRequestFcn
figure
property.
This will let you intercept the requests to delete a figure and then handle those requests as you see fit.
Here is a simple function that illustrates how to use the CloseRequestFcn
to achieve the behavior you are describing above:
function figTest()
f1 = figure('UserData', 1, 'Position', [100 500 50 50]);
f2 = figure('UserData', 2, 'Position', [200 500 50 50]);
set(f1,'CloseRequestFcn', @preCloseMain);
function preCloseMain(src, e)
% Delete f2, in try-block incase it was already closed
try
fprintf('Closing non-main figures!\n');
close(f2);
end
fprintf('Closing main figure!\n');
delete(src);
end
end
Here are the official docs.
Upvotes: 1