MATLAB: Initialize gui

guiguidematlab gui

I'm trying to initialize some parameters/data (defaults) in a GUI and I am using a initialization function in the GUI's opening function to do this. This works fine if I wish to use these settings forever, but I need to be able to change them. So far, I haven't figured out how to get my changes stick. The defaults always get reloaded. Is this because my initialization function is in the gui opening function? Is there another way to initialize a gui? Input args to the gui?

Best Answer

What I do is to load a .mat file in the OpeningFcn function. Check to see if it exists first, otherwise set up your defaults ans create the file. Any time you change something, like the folder you're working in, a scroll bar value, a checkbox, or radio button, etc. Update your "UserSettings" structure and save it out to disk. Like
UserSettings.scrollValue = yourScrollBarValue.
save(yourMatFullFileName, 'UserSettings');
Then in your OpeningFcn:
if exist(yourMatFullFileName, 'file')
recalledSettings = load(yourMatFullFileName);
else
% Make up some defaults for UserSettings
UserSettings.fubar = 42; % Whatever.

UserSettings.scrollValue = 0.69; % Whatever.
% Then:
save(yourMatFullFileName, 'UserSettings');
end