Doug's MATLAB Video Tutorials

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

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.

7 CommentsOldest to Newest

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

@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

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?

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.

Add A Comment

What is 1 + 5?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.