MATLAB: How to cleanly exit from the OpeningFcn of a GUI if a certain condition is met

exitguiMATLABopeningfcnuiresumeuiwait

I would like to stop execution and exit from a GUI from within the OpeningFcn if a
certain input parameter is true, for example, if I call my GUI with an 'exit' flag:
myGUI('exit')

Best Answer

One can typically call the CloseRequestFcn of a GUI to cleanly exit from it. However, calling it directly from the OpeningFcn is not desirable for a clean exit because the GUI is not fully initialized at that point. After exiting from the OpeningFcn the OutputFcn of a GUI is called by the GUIDE execution infrastructure. At the end of this function, the initialization of a GUI is complete and it is safe to call the CloseRequestFcn.
As an example, consider the following implementation of the OpeningFcn and the OutputFcn. This example uses CloseRequestFcn to close the GUI. To generate a default CloseRequestFcn, open the GUI in GUIDE, right-click on the figure and select ViewCallbacks -> CloseRequestFcn. This will open the MATLAB file for the GUI. Modify CloseRequestFcn if needed, then save the MATLAB GUI file.
% --- Executes just before myGUI is made visible.
function myGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB


% handles structure with handles and user data (see GUIDATA)


% varargin command line arguments to myGUI (see VARARGIN)
% Choose default command line output for myGUI
handles.output = hObject;
% if the user supplied 'exit' as an argument, save it in the handles
% structure
if ~isempty(varargin) && ischar(varargin{1}) && strcmp(varargin{1},'exit')
handles.closeFigure = true;
end
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes myGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = myGUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% if the user suppled an 'exit' argument, close the figure by calling
% figure's CloseRequestFcn
if (isfield(handles,'closeFigure') && handles.closeFigure)
figure1_CloseRequestFcn(hObject, eventdata, handles)
end
% - Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
delete(hObject);
The above solution relies on the OutputFcn being called right after the OpeningFcn when adequate inputs are not provided and the GUI must exit. If one wishes to stop the execution of OutputFcn by using UIWAIT, for cases where adequate inputs are provided, a conditional UIWAIT can be placed in the OpeningFcn. The corresponding UIRESUME can be placed in any of other functions as desired.