Save and load all GUIDE GUI variables

4 次查看(过去 30 天)
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?

采纳的回答

Image Analyst
Image Analyst 2012-1-16
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 个评论
Walter Roberson
Walter Roberson 2012-1-16
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
Image Analyst 2012-1-17
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?

请先登录,再进行评论。

更多回答(3 个)

Sean de Wolski
Sean de Wolski 2012-1-16
Check out this video:

Walter Roberson
Walter Roberson 2012-1-16
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.

Jason
Jason 2012-1-16
Thanks everybody! Great advice.

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by