Save and load all GUIDE GUI variables
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Guys -
I built a GUI in GUIDE that's basically a bunch of text edit windows and pushbuttons to call different operations. I'd like to be able to save all the values entered in my text edit windows for later recall when I start a new session with my GUI. Is there a way I can do this by saving all the handles as a single variable as in [guidata(hObject, handles)] and then loading that single variable to populate the text edit windows with those values?
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 16 Jan. 2012
Sure. Have a function called "SaveUserSettings" or something like that. Then call that, for example when you're about to shutdown. Then query each control with get() to get the value and save them to a variable called UserSettings. Save the structure UserSettings to your initialization file, for example mygui.mat. Then in your startup OpenFcn function, call a corresponding "LoadUserSettings" function to read the mat file and send all the values to the various controls with set(). Not that hard to do. I do it all the time with nearly all my apps.
The thing you need to be careful of is if you add a control and then try to read in an old .mat file where the UserSettings structure didn't yet have that member in it. In that case, you can do something like this:
initialValuesS = load(strIniFile);
hasField = isfield(initialValuesS, 'UserSettings');
if ~hasField
% Use set() to send it to the control.
else
% Set up a default value. Then use set() to send it to the control.
end
2 Kommentare
Walter Roberson
am 16 Jan. 2012
Note: the solution I gave does error checking to be sure the handles are valid before trying to save them, and does error checking to be sure that the handles exist before trying to restore the value. My solution also works automatically for everything in handles instead of requiring individual get().
Image Analyst
am 17 Jan. 2012
Good point. But what if I have radio buttons, checkboxes, sliders, static text, axes, push buttons, etc. and you want to save the state of only some of them? I just get the settings for the settings for only the controls I'm interested in. Doesn't your code get the string value for every single control on the window, including ones you don't want or need to save like pushbuttons?
Weitere Antworten (3)
Walter Roberson
am 16 Jan. 2012
No; the values that are stored for handles are double precision values that provide an internal reference to the real data. If you store those double precision values and load them back in a different session, the real data will not exist to be referred to.
Instead, you can use something like this:
Isgraphic = structfun(@(F) isnumeric(F) && length(F) == 1 && ishandle(F), handles);
fnames = fieldnames(handles);
fvals = struct2cell(handles);
structparts = [fnames(Isgraphic).', get([fvals{IsGraphic}],'String').'];
saved_edit_fields = struct(structparts{:});
and save saved_edit_fields
Then when you load:
fnames = fieldnames(saved_edit_fields);
fvals = struct2cell(saved_edit_fields);
Isgraphic = cellfun(@(F) isfield(handles.(F)) && ishandle(handles.(F)), fnames);
for K = find(Isgraphic)
set(handles.(fnames{K}), 'String', fvals{K});
end
Or reasonable hand-drawn facsimile thereof.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!