MATLAB: Guidance on Closing a “GUIDE” Gui

closedeleteMATLABuiresume

Hello!
I have been trying to cleanly exit a GUIDE gui, and am a bit confused as to the proper sequence. The OpeningFcn is straighforward, but I don't know about a couple of things:
  1. If you have to close the gui right away (like a security check put in the OpeningFcn isn't met), how do you exit? return()? or call the close function? The close has a modal dialog to see if you want to close, but we don't want to do that if security doesn't pass.
  2. What is the proper sequence to (a) close any open windows that have been created during the session and (b) if you have called uiwait, when do you do the delete(handles.figure1) and the uiresume(handles.figure1)? Or other proper close functions?
Thanks.
Doug

Best Answer

I would recommend putting any checks in a function that itself opens the GUI then you simply don't even attempt to open the GUI if the checks fail. I have been caught out myself with bailing out on a GUI in the OpeningFcn and ending up with a ghost GUI that is in some hidden state.
closereq
should do the job though to close the window although not if you have put things in the CloseRequest function that you do not want to happen in this case.
As far as closing windows that have been opened during a session goes it is entirely down to your application really. As far as I know Matlab does not allow figures to be parented by other figures so I programmed a GUI Manager class that deals with launching GUIs and closes them all down automatically when the manager is deleted (which happens when the 'parent' window closes). Generally though you can just close them yourself in the DeleteFcn or CloseRequestFcn of your main window.
If you call uiwait with a figure handle you don't need an explicit uiresume so long as the figure is closed at some point as this will cause it to resume anyway. You can do a uiresume explicitly though to pair up with a uiwait that was not given a figure handle. Then you can delete the figure whenever you deem appropriate.