Where should user preferences be stored?

13 visualizaciones (últimos 30 días)
Daniel Dolan
Daniel Dolan el 9 de Nov. de 2015
Editada: Daniel Dolan el 9 de Nov. de 2022
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.
  2 comentarios
Marcel
Marcel el 9 de Nov. de 2022
Take it with humor :P
Daniel Dolan
Daniel Dolan el 9 de Nov. de 2022
Editada: Daniel Dolan el 9 de Nov. de 2022
I had forgotten about this question until today and realized that I had been using the newly accepted solution for some time.
The picture is hilarious.

Iniciar sesión para comentar.

Respuesta aceptada

Daniel Dolan
Daniel Dolan el 9 de Nov. de 2022
MATLAB's setpref/getpref commands are the best solution on specific machine, and they persist across sessions.

Más respuestas (2)

Image Analyst
Image Analyst el 9 de Nov. de 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.

Marcel
Marcel el 9 de Nov. de 2022
Editada: Marcel el 9 de Nov. de 2022
I used to save files in a xml like config file (.txt) and store stuff like the color settings of my application as well as other data that i'd need permanently.
I also load "modules" with my app and they have data that needs to be stored too, and since they change a lot and are "modules" i saved them as a matlab file (.m).
It depends on what you need and what you want. sure the xml like config file could've been a matlab file too. no doubt, but thats just how i started my app, and i kinda prefer it actually :P

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by