MATLAB Answers

Where should user preferences be stored?

21 views (last 30 days)
Suppose you have a toolbox that recognizes a variety of user preferences that are to be reused throughout a MATLAB session. Where should these preferences be stored?
Global variables seem like an easy path, but they are reset by "clear all". You could hide the same information in the application-defined data of the root object, which is shielded from "clear all" during the current session. Preferences could also be stored in a hidden file and preserved in future sessions.
I've been using the second approach (set/get appdata), but I'm curious what other approaches have bee tried.

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 9 Nov 2015
I save user preferences to a .mat file on disk. Unlike globals, persistents, and get/setappdata, they will survive different sessions of MATLAB, and can even be migrated to a different computer. Plus, if you're on Windows and save them to the C:\Users\Public\AppData\MATLAB folder the results will be the same for all users on a multi-user system, which can be important for consistency. (I've had mixed success storing them where Microsoft want you to, the c:\ProgramData folder.) You may not want different users running your app with different settings for certain parameters, such as filter window size or minimum allowable blob area or whatever, or else they won't get the same answers on the same input data. Of course if you want each user to have their own personal set of parameters, then you can do that too, just store under the user's folder.
I have a LoadUserSettings() function, that gets called when the app launches, and a SaveUserSettings() function that gets called when the user shuts down the app or does some other activity where you want to save it right away, such as changing the folder where their data is or whatever. LoadUserSettings() will read in the mat file, which has the GUI setings, and then use set() to send those gui settings to the proper GUI control/widget. SaveUserSettings() does the opposite, it collects parameters that I want to save from the various controls, and saves a single structure variable that I call UserSettings, to a mat file. UserSettings has different field, like UserSettings.checkboxValue1 and UserSettings.editString1, that hold the different properties of the different controls. But just the ones I want to save - I don't save everything under the sun.
Of course this works perfectly well within a single MATLAB session, like you asked for, but is more robust in that it also works across sessions or even on different computers.

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!