Changing system preferences programmatically

My very first post on this blog showed how to change Matlab’s command-window colors. In that post I promised to write another post detailing how system preferences can be changed from the command line.

Last week I wrote about undocumented GUIDE features. After my initial post, I realized that I forgot to mention all the different relevant system preferences that can also be modified. I therefore modified last week’s post with the preferences information. I now wish to finally detail how such preferences can be changed programmatically, from within your Matlab application or from the Matlab desktop command prompt.

Matlab’s user preferences are stored in the matlab.prf text file which is stored in the user’s Matlab preferences folder:

edit(fullfile(prefdir,'matlab.prf'));

Each preference appears to be on a separate line in the following format: <pref-name>=<pref-type><pref-value>

where <pref-type> appears to be one of these:

  • ‘B’ => boolean/logical flag
  • ‘C’ => color (RGB numeric value)
  • ‘F’ => font (type,size,name)
  • ‘I’ => int16
  • ‘J’ => int64
  • ‘R’ => rectangular area (x,y,h,w)
  • ‘S’ => string/char

Examples:
    EditorShowLineNumbers=Btrue
    EditorMaxCommentWidth=I120

You can read the preference names from this matlab.prf file and then use the following (you-guessed-it) java calls to get/set the values:

com.mathworks.services.Prefs.get<type>Pref(<pref-name>)
com.mathworks.services.Prefs.set<type>Pref(<pref-name>, newValue);

where <type> is one of: Boolean, Color, RGBColor, Font, Integer, Rectangle, String, Double (I believe Doubles get converted to int64 - possibly a bitwise casting since both use 64 bits)

For example:

com.mathworks.services.Prefs.getBooleanPref('LayoutSnapToGrid')
com.mathworks.services.Prefs.setIntegerPref('LayoutGridWidth', 25)

adding a second argument to get<type>Pref() appears to indicate a default value that is returned if <pref-name> is not defined:

com.mathworks.services.Prefs.getIntegerPref('xxxx',123)
=> 123

We can programmatically set any preference key we like - we are not limited to Matlab’s built-in set. I used this feature in my CPRINTF utility, to set user-defined colors for later use by the desktop’s UI syntax-highlighting engine. The relevant code segment is this:

% Convert a Matlab RGB vector into a known style name (e.g., '[255,37,0]')
function styleName = getColorStyle(rgb)
 
  % Convert Matlab RGB array into a Java Color object
  intColor = int32(rgb*255);
  javaColor = java.awt.Color(intColor(1), intColor(2), intColor(3));
 
  % Preference key name format: '[RRR,GGG,BBB]'
  styleName = sprintf('[%d,%d,%d]',intColor);
 
  % Set/update the preference with this Java Color
  com.mathworks.services.Prefs.setColorPref(styleName,javaColor);

…which in turn adds entries such as the following to my matlab.prf file:
    [12,34,67]=C-15982013

Note that -15982013 = 0xFF0C2243, which is the RGB value [12,34,67] with an opaque alpha value. This value can later be retrieved using:

>> com.mathworks.services.Prefs.getColorPref('[12,34,67]')
ans =
java.awt.Color[r=12,g=34,b=67]

Warning: I published much of this information on the CSSM forum back in 2007. Ben Steiner then shared his experience on that thread that:

For anyone else that’s playing with this: I don’t advise trying to edit the matlab.prf via matlab(!). I created a situation that made Matlab unworkable. I did find that deleting the matlab.prf completely (in frustration) solved it.

Take a look at your matlab.prf file - can you spot any interesting preference? If so, please share it in the comments section below.

Bookmark and Share

Related posts:

  1. Changing Matlab’s Command Window colors Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....
  2. Setting the Matlab desktop layout programmatically The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....
  3. Changing Matlab’s Command Window colors - part 2 The Matlab Command Window enables a limited degree of inline color customization - this post describes how to use it...
  4. R2009b keyboard bindings The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its variants....
  5. cprintf - display formatted color text in the Command Window cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...
  6. setPrompt - Setting the Matlab Desktop prompt The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...

Tags: ,

PoorSo-soHelpfulVery helpfulExcellent! (1 votes, average: 4.00 out of 5)
Loading ... Loading ...
Bookmark and Share Print Print

Leave a Reply


Wrap code fragments inside <pre lang="matlab"> tags, like this:

   <pre lang="matlab">
   a = magic(3);
   sum(a)
   </pre>