Stuart’s MATLAB Videos

Watch and Learn

How to save and restore state of a GUI in MATLAB 11

Posted by Doug Hull,

When constructing a GUI, sometimes it would be appropriate for the GUI to start out in the last state it was in when it was closed. This means the same values selected in list boxes, same radio buttons clicked etc…

This video shows how to go and capture the state of the GUI, store it for later use and then restore it.

11 CommentsOldest to Newest

yan replied on : 2 of 11
In a GUI, How can I stop one program running without exiting the GUI (and also keep the current GUI state)? In my programs, I only can stop programs running with 'ctrl + c' to kill the programs, Anyway I can dot it? Thanks
Lior Cohen replied on : 3 of 11
I published a while ago, in the file exchange, a uiremember and uirestore functions that make it very easily (does all the work for you). Here is the link -
dhull replied on : 4 of 11
@yan, ctrl-c will stop running MATLAB programs. A GUI is a running MATLAB program. If one of the GUI callbacks is being run when you hit ctrl-c, that is what you will break out of, but the gui will still be running. I am not sure what you are asking for. What behavior are you seeing now? Doug
Devin replied on : 5 of 11
My gui only has
CloseMenuItem_Callback(hObject, eventdata, handles)
Which works fine when I click file>close, but doesn't get called when I just click the X in the upper right hand corner. I tried adding a
CloseRequestFcn_Callback(hObject, eventdata, handles)
But that never gets called. Am I using a different type of GUI? How do I add a CloseRequestFcn that actually gets called?
Brett replied on : 7 of 11
I required the functionality of saving and restoring the state of a gui and approached the problem in a similar fashion to what Lior Cohen suggested above and posted on the file exchange. I looped through the handles structure of the current gui using
ishandle to weed out non graphics handle fields and 
ismember(get(h.(handleName{i}),'type'),{'uicontrol','uimenu','uitable'}) to look for gui components with readily changeable values.  Then I used switch-case structures to filter the "type" and for uicontrols the "style" in order to determine which properties to save/restore.

This is basically the same functionality offered by Lior's code on the File Exchange, though Lior's only looks at uicontrols and doesn't handle uitables or the "checked" property of uimenus.  Lior's code does have the advantage of offering custom options through inputs to the remember function.

Another common gui state to save and restore is the position, which is a property of the figure itself instead of any of the child components.

In some cases if a sub-GUI (like a settings dialog) is being dismissed and may be called up again later by the parent GUI it may be possible to simply hide it by setting 'Visible'='off' in the close function and then the next time the sub-GUI is called by the main GUI, the main GUI checks to see if the sub-GUI is already active and if so simply sets 'Visible'='on'.  Of course the close function of the main GUI would need to include code to close any lingering sub-GUI's that could still be hidden.                
envasadora al vacio replied on : 8 of 11
Thanks in favor of sharing such a pleasant opinion, post is pleasant, thats why i have read it fully
Boby G replied on : 9 of 11
I have created a guide with button ... etc. but I delated the fig.file accidently. How can I restore the fig file? Thank you
Hi Boby, You have no file backup on your computer? I'm not sure if it is possible to regenerate from the M file as I think information is stored in it that is not stored in the code. I'd recommend confirming with tech support:
Oskar Adolfson replied on : 11 of 11
What a great video! Is it maybe possible to update it in favor to make it applicable in App Designer? Many thanks!