{"id":379,"date":"2010-05-31T17:57:21","date_gmt":"2010-05-31T17:57:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2010\/05\/31\/command-line-preferences\/"},"modified":"2010-05-31T17:57:21","modified_gmt":"2010-05-31T17:57:21","slug":"command-line-preferences","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2010\/05\/31\/command-line-preferences\/","title":{"rendered":"Command-Line Preferences"},"content":{"rendered":"<p>I received some criticism regarding my post about <a href=\"https:\/\/blogs.mathworks.com\/community\/2010\/05\/17\/calling-shell-commands-from-matlab\/\">using shell command<\/a> because it wasn&#8217;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&#8217;m mentioning this to ward off any similar comments for this post about getting\/setting preferences from the command line. To be clear I&#8217;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 <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/getpref.html\"><tt>getpref<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/setpref.html\"><tt>setpref<\/tt><\/a> methods.<\/p>\n<p>To use the MATLAB preferences functions you need to come up with a &#8220;group&#8221; 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 <a href=\"https:\/\/blogs.mathworks.com\/community\/2010\/01\/11\/the-preferences-folder-matlabprf\/\">matlab.prf<\/a> 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 <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/prefdir.html\"><tt>prefdir<\/tt><\/a>.<\/p>\n<p><strong>Here is an example where I store the URL of my blog as a preference:<\/strong><\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">%% get the pref with the default value<\/span>\r\nispref(<span style=\"color: #A020F0\">'desktop_blog'<\/span>,<span style=\"color: #A020F0\">'url'<\/span>)\r\n<span style=\"color: #228B22\">% getpref(PREF_GROUP, PREF_NAME, DEFAULT_VALUE)<\/span>\r\ngetpref(<span style=\"color: #A020F0\">'desktop_blog'<\/span>,<span style=\"color: #A020F0\">'url'<\/span>,<span style=\"color: #A020F0\">'https:\/\/blogs.mathworks.com\/desktop'<\/span>)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     0\r\n\r\n\r\nans =\r\n\r\nhttps:\/\/blogs.mathworks.com\/desktop\r\n\r\n<\/pre>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">%% change the pref to point to the MATLAB Central Main page<\/span>\r\nsetpref(<span style=\"color: #A020F0\">'desktop_blog'<\/span>,<span style=\"color: #A020F0\">'url'<\/span>,<span style=\"color: #A020F0\">'http:\/\/www.matlabcentral.com'<\/span>)\r\nispref(<span style=\"color: #A020F0\">'desktop_blog'<\/span>,<span style=\"color: #A020F0\">'url'<\/span>)\r\ngetpref(<span style=\"color: #A020F0\">'desktop_blog'<\/span>,<span style=\"color: #A020F0\">'url'<\/span>,<span style=\"color: #A020F0\">'https:\/\/blogs.mathworks.com\/desktop'<\/span>)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     1\r\n\r\n\r\nans =\r\n\r\nhttp:\/\/www.matlabcentral.com\r\n\r\n<\/pre>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">%% reset the pref<\/span>\r\nrmpref(<span style=\"color: #A020F0\">'desktop_blog'<\/span>,<span style=\"color: #A020F0\">'url'<\/span>)\r\nispref(<span style=\"color: #A020F0\">'desktop_blog'<\/span>,<span style=\"color: #A020F0\">'url'<\/span>)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     0\r\n\r\n<\/pre>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">%% how might this be used?<\/span>\r\nweb(getpref(<span style=\"color: #A020F0\">'desktop_blog'<\/span>,<span style=\"color: #A020F0\">'url'<\/span>,<span style=\"color: #A020F0\">'https:\/\/blogs.mathworks.com\/desktop'<\/span>))<\/pre>\n<p>Just so you don&#8217;t get confused when looking around the documentation, there&#8217;s also an <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/addpref.html\"><tt>addpref<\/tt><\/a> which as far as I can tell does the same thing as <tt>setpref<\/tt> except throw an exception if the preference group and name already exist. There&#8217;s also a <tt>ui(get\/set)pref<\/tt> 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 <a href=\"https:\/\/blogs.mathworks.com\/community\/2009\/08\/03\/more-dialogs-than-you-can-shake-a-stick-at\/\">dialog functions<\/a>.<\/p>\n<p>David Schwartz, one of <a href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/03\/03\/whats-in-your-startupm\/#comment-30084\">Loren&#8217;s readers<\/a> uses <tt>getpref<\/tt> to save\/restore the last current working directory between MATLAB sessions. How do you or might you use these preferences functions?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I received some criticism regarding my post about using shell command because it wasn&#8217;t very Desktop-y. The way I see it, the MATLAB Environment includes a broader picture of everything going&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2010\/05\/31\/command-line-preferences\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/379"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=379"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/379\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}