Where should user preferences be stored?
조회 수: 12 (최근 30일)
이전 댓글 표시
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
채택된 답변
추가 답변 (2개)
Image Analyst
2015년 11월 9일
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
Marcel
2022년 11월 9일
편집: Marcel
2022년 11월 9일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!