I created a generic modal question dialog using GUIDE. It's a simple yes/no question dialog. If I try to close the window using the x in the top right corner rather than clicking a button, when it gets to varargout{1} = handles.output I get the error "Attempt to reference field of non-structure array." If I click one of the buttons it works fine, it's only if I try to close the window without clicking a button. I'm using MATLAB R2015a. I didn't change anything, this is the generic yes/no question dialog pre-built in GUIDE. By the way, this is in the OutputFcn. I have a gui I set up similarly as far as OpeningFcn and OuptputFcn go, which is how I discovered the problem in the first place. I noticed they were set up similarly and decided to try the built-in question dialog to see what the difference was when I found out it does the same thing. I attached the m file for my project.
MATLAB: Generic modal question dialog error on close
error close windowMATLAB
Related Solutions
Note what has to be present in the close request function.
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.
Best Answer