Comments on: Video: Saving the state of a GUI https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/?s_tid=feedtopost Stuart uses video to share his experiences solving problems with MATLAB day-to-day, interesting new features, plus tips and tricks he has picked up along the way. Fri, 15 Jan 2016 15:32:53 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Raja https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3066 Thu, 03 May 2012 15:00:32 +0000 https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3066 @Matt

If the variables that you are loading are properties of an object then it is possible to add a listener to this object property. The listener allows you to add callback to update GUI when ever this variable changes.

properties (SetObservable, GetObservable, AbortSet)
PropOne = 7
end

]]>
By: Doug https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3062 Tue, 24 Apr 2012 20:19:07 +0000 https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3062 @Matt,

Sorry, but the inputs are not ‘noticed’ by MATLAB until Enter is pressed, or the focus is changed from that control.

Doug

]]>
By: Matt https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3061 Tue, 24 Apr 2012 18:20:56 +0000 https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3061 Hello !

Thank you very much for your code, it is very useful ! Maybe I ask something ? How can I do so that when the user inputs are loaded back in the fields of the GUI, they automatically update the workspace without having to press ENTER in every field ?

Thank you very much again ! :=)

]]>
By: Jason https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3020 Fri, 09 Mar 2012 17:04:31 +0000 https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3020 I have a callback function called saveMAT that looks like:

function saveMAT_Callback(hObject, eventdata, handles)
SessionName = evalin(‘base’,’SessionName’);
filename = [SessionName ‘.txt’];
fidOUT = fopen(filename, ‘a’);
h = waitbar(0.5,’Saving workspace data…’);
guidata(hObject, handles);
getstate(handles,1);
evalin(‘base’, [‘save(”’, SessionName ”’)’]);
set(handles.EndSessionButton,’Enable’,’on’);
newline = ‘———————————————-‘;
newline2 = char([‘Master File ‘, SessionName, ‘.mat created/updated.’]);
fprintf(fidOUT,’%s\n’,newline,newline2);
fclose(fidOUT);
delete(h);
clc;

The function getstate saves all the handles I want to restore into a structure variable ‘state’ and saves the variable as a .mat file, as you describe in your videos. It does so by doing this repeatedly:

state.SessionName = get(handles.SessionName);
state.createsession = get(handles.createsession);
state.openoldsession = get(handles.openoldsession);
state.LoadData = get(handles.LoadData);

so that the structure state.* contains all the handle data for GUI element *.

The problem arises when I start a new MATLAB session and I want to restore the GUI using the handle data in ‘state’. When the GUI is initialized, the handles are just floating point numbers, not pointers to elements in my GUI. So, when I try a command like set(handles.*,state.*) I get an error that says that handles.* is not a valid handle structure. Given that I store all the handle information from my previous session in ‘state’, how then do I restore this data to the GUI when it opens after the beginning of a new MATLAB session?

]]>
By: Doug https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3019 Thu, 08 Mar 2012 16:02:43 +0000 https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3019 @Jason,

Nothing we are doing here needs the same handles structure. We are only saving the values, not the specific handles. What are you storing in the MATLAB file?

]]>
By: Jason https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3015 Wed, 07 Mar 2012 23:53:34 +0000 https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-3015 Hi Doug –

I’m having a lot of difficulty trying to code a way to not only save and restore the state of a GUI in the same session (as you illustrate here), but to restore the state of a GUI after starting a NEW Matlab session. The approach you describe here seems to fail when in a new Matlab session because the handles data from the previous session can’t be restored. Is there a way to apply a GUI restoration method that will work within a new/different Matlab session? Thanks for any help you can provide.

]]>
By: dhull https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-2719 Tue, 19 Jul 2011 20:25:10 +0000 https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-2719 @Lekshmi,

It all depends what state.bmatrix and handles.private.bmatrix are! Please elaborate.

Doug

]]>
By: Lekshmi https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-2699 Thu, 07 Jul 2011 16:22:02 +0000 https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-2699 Hi Doug,

I am able to save and restore the states of all gui objects except the private handle.

for eg: handles.private.bmatrix.
I can save the state, but unable to restore.

here goes my code;

function saveState(handles)

state.resliced_images_edit = get(handles.resliced_images_edit, ‘String’);
state.maskimage_radiobutton = get(handles.maskimage_radiobutton,’Value’);

state.bmatrix = handles.private.bmatrix;

save images.mat state;
—————————————————-
function restoreState(handles)

load ‘images.mat’ ‘state’
set(handles.resliced_images_edit, ‘String’,state.resliced_images_edit);
set(handles.maskimage_radiobutton,’Value’,state.maskimage_radiobutton);
handles.private.bmatrix = state.bmatrix;
———————————————————

Please help!

Thanks in advance!

Lekshmi

]]>
By: Catalin Lupu https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-1281 Mon, 29 Dec 2008 12:51:03 +0000 https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-1281 Hi Doug,

First of all thank you for your reply and I apologize for the late answer.

In the beginning I have created this classes with the idea that eventually I will used them in different projects and this is why I have used FINDOBJ. Initially I was afraid to send the handle structure because at runtime will hold a lot of data and I considered that the application will use a lot of memory (I am still not sure about that one).

So I could execute this line:

handlesFields = fieldnames(handles); % extract all the field names in handles

before I call the function listed above. In this way the function code is independent of the handles structure presence.

I did not use GETAPPDATA and SETAPPDATA because I found it useless (when I started programming GUIs). From the documentation I understood that the UserData is the field that holds this additional data for each widget that I have. But in the first place I usually need the string or the current value of the widget so why should I save something in the UserData? I feel like I am missing something here!

I will take a closer look at the things that you suggested. The use of handle structure instead of FINDOBJ is definitely something that I will consider because I know it take time to find the objects.

And one more thing! I am doing something like:

handles.varA = varAClass; % create an instance of a class

Where varA is an instance of the class varAClass. In this way varA is accessible from any callback function in the GUI.

Could I do something like:

varATmp = varAClass; % create a temporary instance
setappdata(figurehandle,’varA’,varATmp); % store the data in the appdata.

If this is a possibility then what will happen with the class methods? How do I have access to the class instance? Do I need to use getappdata or I just use the variable name (in this case varA.methodA())? Do I still have to use a call like handle.varA?

I know that this post is taking another root that the original but I do not know where to continue the discussion. If you have any suggestions please say so.

Thank you again for the help and the time spent on my post!

Regards and Happy New Year (it is almost time for that :))

Catalin

]]>
By: dhull https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-1239 Fri, 21 Nov 2008 16:46:06 +0000 https://blogs.mathworks.com/videos/2008/10/31/video-saving-the-state-of-a-gui/#comment-1239 Caitlin,

It seems like you have a pretty good idea of what is going on here already. I think that you can speed your code by avoiding the use of FINDOBJ, you already have the handle to it in the handles structure.

I think this is the pseudo code implementation that I would recommend:

for i = 1 : numel(handles)
storedHandle.val = get(handles.name, ‘value’)
other relevent states, like string, there should only be a few you need to capture.
end

Other states that your GUI might have seems to be the current tab. You apparently have a tab implementation of some sort, so that data should be stored somewhere also (I expect you are using GETAPPDATA and SETAPPDATA for this) So store that value also.

As part of your “restore function” I would expect that you could call the “Go to this Tab” function and that would reset the state also.

By removing the findObj, you will likely find this code goes way faster, and I think that will help make the other problems easier to deal with.

-Doug

]]>