MATLAB Community

MATLAB, community & more

Command-Line Preferences

I received some criticism regarding my post aboutusing shell commandbecause it wasn’t very Desktop-y. The way I see it, the MATLAB Environment includes a broader picture of everything going on around MATLAB, which includes not only the Desktop GUI but also operating system, and anything else you can access. I’m mentioning this to ward off any similar comments for this post about getting/setting preferences from the command line. To be clear I’m not talking about programmatically setting the Desktop preferences that are controlled from the Preferences GUI, but rather how to add persistent preferences to your own programs through thegetprefandsetprefmethods.

To use the MATLAB preferences functions you need to come up with a “group” name and a preference name. Think of the group as a the toolbox name if you were making your own toolbox. The preference name is the unique key within that group. Unlike the desktop preferences which are stored in thematlab.prftext file, the values used by preference mechanism are stored in a MAT-file (matlabprefs.mat). This means that the preferences can be any MATLAB value, not just strings and scalars. Like matlab.prf, the matlabprefs.mat file is stored in yourprefdir.

Here is an example where I store the URL of my blog as a preference:

%% get the pref with the default valueispref('desktop_blog','url')% getpref(PREF_GROUP, PREF_NAME, DEFAULT_VALUE)getpref('desktop_blog','url','https://blogs.mathworks.com/desktop')
ans = 0 ans = https://blogs.mathworks.com/desktop
%% change the pref to point to the MATLAB Central Main pagesetpref('desktop_blog','url','http://www.matlabcentral.com') ispref('desktop_blog','url') getpref('desktop_blog','url','https://blogs.mathworks.com/desktop')
ans = 1 ans = http://www.matlabcentral.com
%% reset the prefrmpref('desktop_blog','url') ispref('desktop_blog','url')
ans = 0
%% how might this be used?web(getpref('desktop_blog','url','https://blogs.mathworks.com/desktop'))

Just so you don’t get confused when looking around the documentation, there’s also anaddprefwhich as far as I can tell does the same thing assetprefexcept throw an exception if the preference group and name already exist. There’s also aui(get/set)prefwhich seems to me to just be a generic dialog that the user can set a preference whether or not that specific dialog is shown again; the command does not explicitly create a dialog to get or set a specific preference. You can easily build such a UI with the (get/set)pref functions and GUIDE or any of thedialog functions.

David Schwartz, one ofLoren’s readersusesgetprefto save/restore the last current working directory between MATLAB sessions. How do you or might you use these preferences functions?

|
  • print
  • send email

Comments

To leave a comment, please clickhereto sign in to your MathWorks Account or create a new one.