Mike on the MATLAB Desktop
May 31st, 2010
Command-Line Preferences
I received some criticism regarding my post about using shell command because 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 the getpref and setpref methods.
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 the matlab.prf text 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 your prefdir.
Here is an example where I store the URL of my blog as a preference:
%% get the pref with the default value
ispref('desktop_blog','url')
% getpref(PREF_GROUP, PREF_NAME, DEFAULT_VALUE)
getpref('desktop_blog','url','http://blogs.mathworks.com/desktop')
ans =
0
ans =
http://blogs.mathworks.com/desktop
%% change the pref to point to the MATLAB Central Main page
setpref('desktop_blog','url','http://www.matlabcentral.com')
ispref('desktop_blog','url')
getpref('desktop_blog','url','http://blogs.mathworks.com/desktop')
ans =
1
ans =
http://www.matlabcentral.com
%% reset the pref
rmpref('desktop_blog','url')
ispref('desktop_blog','url')
ans =
0
%% how might this be used?
web(getpref('desktop_blog','url','http://blogs.mathworks.com/desktop'))
Just so you don’t get confused when looking around the documentation, there’s also an addpref which as far as I can tell does the same thing as setpref except throw an exception if the preference group and name already exist. There’s also a ui(get/set)pref which 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 the dialog functions.
David Schwartz, one of Loren’s readers uses getpref to save/restore the last current working directory between MATLAB sessions. How do you or might you use these preferences functions?
By
Michael Katz
Mike is a developer on the MATLAB Mobile team. When not describing himself in the third person, biking, homebrewing, or rooting for the home team, he's busy trying to make the world a better place for programming.
17:57 UTC |
Posted in Preferences |
Permalink |
4 Comments »
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
|
I use preferences and uigetpref in a user interface that we have for our toolbox. Specifically uigetpref gives the user some control in what warning messages get displayed. If the value of the preference is initially set to ‘ask’ then uigetpref displays a dialog box with choices and a checkbox allowing the user to suppress the particular message in the future. For example:
setpref('mytoolbox','baddata','ask') ... if (baddata) pref = uigetpref('mytoolbox','baddata','Bad Data','Bad Data Found. Proceed?',{'Yes','No'}); endIf the preference value is something other than ‘ask’, uigetpref returns the value. If the user checked the checkbox last time uigetpref was called, it returns the previously selected value without opening the dialog box.
I use getpref and setpref in some of my File Exchange utilities, when asking users whether they wish to download and use a newer version that is available on the File Exchange. If the user requests not to be asked again, her request is duly noted in the appropriate pref item. When the user then uses the utility again, getpref detects the preference and does not disturb her again with the download question.
It should be noted that modern Matlab releases have the uigetpref utility which greatly facilitate this functionality by having a “don’t ask again” checkbox.
@Amy,
Yep, that is what uigetpref is for. The confusing thing for me is that a dialog like uisetcolor and uisetfont are used to let the user select a particular data value. Whereas the preference in uigetpref is about whether that dialog is shown again or not, it’s not for changing a preference (for example: a gui background color).
I am continously building (image and data processing) functions which eventually make their way to a more user-friendly GUI. In those GUIs, I use addpref to keep track of the user-selectable values (debug level, last folder, other configuration). As Mike, I am not a real fan of the uigetpref command.