Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Changing system preferences programmatically

June 17, 2009 17 Comments

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'));

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);

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)

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

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);

% 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]

>> 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.

Related posts:

  1. 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....
  2. 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....
  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. Removing user preferences from deployed apps – An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. ...
  5. 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....
  6. 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...
Desktop Java
Print Print
« Previous
Next »
17 Responses
  1. Undocumented feature() function | Undocumented Matlab May 4, 2010 at 09:21 Reply

    […] feature(’GetPref’,prefName) – returns the system preference value for the requested preference name in the global prefs file ([prefdir,’matlab.prf’]) that is explained here. […]

  2. Shaul February 2, 2011 at 08:07 Reply

    Hi Yair,

    I work on multiple projects at the same time and I like to have multiple matlab instances open on my computer, one for each project (I realize this might not be a particularly smart way to work). However, I would like each of them to save to a different command window history file. Is there a good safe way to do this?

    Thanks! All the best,
    Shaul

    • Yair Altman February 2, 2011 at 09:54 Reply

      @Shaul – I do not know a way to customize the history file path

  3. Paul May 25, 2011 at 13:33 Reply

    Is there a way to programmatically update the Simulink preferences, such as the Autosave option?

    Thanks,

    paul

    • Yair Altman May 25, 2011 at 13:38 Reply

      @Paul – I’m sure there is, but I don’t know…

  4. Chad Gilbert January 10, 2012 at 12:13 Reply

    Hi Yair,

    It seems that after doign this, I need to re-start MATLAB before the changes take effect. Do you know of any better way to make my changes “kick in”?

    • Yair Altman January 10, 2012 at 12:56 Reply

      @Chad – there is a com.mathworks.services.Prefs.load() method, but I think it will not help you much because I think that most preferences are loaded and used during startup, not dynamically.

  5. Jonas November 14, 2012 at 15:56 Reply

    I just learned once more that it’s extremely helpful to programmatically check for the correct “MatfileSaveFormat” property to avoid being unable to save (or later: load) your results. Thanks for your helpful post!

  6. Oleg Komarov October 13, 2014 at 03:08 Reply

    To change Parallel Pool settings:

    ps = parallel.Settings;
    ps.Pool
    ans = 
      PoolSettings with properties:
                                AutoCreate: 0
                    RestartOnClusterChange: 1
        RestartOnPreferredNumWorkersChange: 1
                               IdleTimeout: 30
                       PreferredNumWorkers: 12

    ps = parallel.Settings; ps.Pool ans = PoolSettings with properties: AutoCreate: 0 RestartOnClusterChange: 1 RestartOnPreferredNumWorkersChange: 1 IdleTimeout: 30 PreferredNumWorkers: 12

    • Yair Altman October 13, 2014 at 04:24 Reply

      To complete the picture:

      >> ps = parallel.Settings
      ps = 
       Parallel Settings Information
             DefaultProfile: local
       
          Profiles and Components: 
       
                   Profiles: [1x1 parallel.settings.Profile]
        SchedulerComponents: [1x1 parallel.settings.schedulerComponent.Local]
          ProjectComponents: [0x0 parallel.settings.ProjectComponent]
       
      >> getundoc(ps)
      ans = 
                  ConfigsMcrUserDataKey: 'ParallelConfigurationFile'
          ConfigurationsLastUpgradeTime: '???'
          DefaultProfileChangedListener: [1x1 event.proplistener]
                DefaultProfileListeners: [1x2 event.listener]
                   DefaultProfileObject: [1x1 parallel.settings.Profile]
                 DummyEnforceInitClient: 'BMPA'
                       ImportNameSuffix: '_Import'
               LastUpgradeSettingsLevel: [1x1 parallel.internal.types.SettingsLevel]
                       LocalProfileName: 'local'
                  McrUserDataNameSuffix: '_mcruserdata'
                             NodeFacade: [1x1 parallel.internal.settings.SettingsNodeFacade]
                                   Pool: [1x1 parallel.internal.settings.PoolSettings]
                     ProfilesCollection: [1x1 parallel.internal.settings.NamedNodesCollection]
                 ProfilesMcrUserDataKey: 'ParallelProfile'
            ProjectComponentsCollection: [1x1 parallel.internal.settings.NamedNodesCollection]
          SchedulerComponentsCollection: [1x1 parallel.internal.settings.TypedNodesCollection]
               UsingDeployedCollections: 0

      >> ps = parallel.Settings ps = Parallel Settings Information DefaultProfile: local Profiles and Components: Profiles: [1x1 parallel.settings.Profile] SchedulerComponents: [1x1 parallel.settings.schedulerComponent.Local] ProjectComponents: [0x0 parallel.settings.ProjectComponent] >> getundoc(ps) ans = ConfigsMcrUserDataKey: 'ParallelConfigurationFile' ConfigurationsLastUpgradeTime: '???' DefaultProfileChangedListener: [1x1 event.proplistener] DefaultProfileListeners: [1x2 event.listener] DefaultProfileObject: [1x1 parallel.settings.Profile] DummyEnforceInitClient: 'BMPA' ImportNameSuffix: '_Import' LastUpgradeSettingsLevel: [1x1 parallel.internal.types.SettingsLevel] LocalProfileName: 'local' McrUserDataNameSuffix: '_mcruserdata' NodeFacade: [1x1 parallel.internal.settings.SettingsNodeFacade] Pool: [1x1 parallel.internal.settings.PoolSettings] ProfilesCollection: [1x1 parallel.internal.settings.NamedNodesCollection] ProfilesMcrUserDataKey: 'ParallelProfile' ProjectComponentsCollection: [1x1 parallel.internal.settings.NamedNodesCollection] SchedulerComponentsCollection: [1x1 parallel.internal.settings.TypedNodesCollection] UsingDeployedCollections: 0

  7. Scott Lowe November 8, 2015 at 21:00 Reply

    Hi Yair,

    Thanks for this article!

    I took inspiration from it to create a package to import and export color schemes in MATLAB. This definitely would not have been possible without undocumentedmatlab!

    It is available on GitHub and FileExchange.

    All the best,
    Scott

  8. jon May 13, 2016 at 20:06 Reply

    Thank you!

  9. Parvaneh September 28, 2016 at 13:59 Reply

    Hi Yair,

    I was looking for a way to change the setting of the system using matlab when I saw this post.
    I wonder if you can help me with it.
    I’m connecting two monitors and I want to run a program from one monitor and make it run on the other one.
    In the process I need to change setting from ‘screen resolution’ in the control panel.
    Could this be done with matlab?

    I would appreciate any help.

    Thanks,
    Parvaneh

    • Yair Altman September 28, 2016 at 14:09 Reply

      @Parvaneh – you will need to check the web for a utility that has a command-line interface that does what you want, and then you can call that utility from Matlab using the system command.

  10. Mohit May 22, 2020 at 15:11 Reply

    Hi Yair,
    I am trying to change keybindings to certain keys for many systems is there any way that we can change these settings.
    i can change in window dialogue like Home>Environment>Preferences>keyboard>Shortcuts > then i select shortcut and change ….can we do it through code like above ? I tried many things but didn’t able get it to work

  11. Robin October 30, 2020 at 14:49 Reply

    Hi Yair,

    This has been a great help.
    With the removal of com.mathworks sometime in the future (as announced in R2020b), is there an alternative?
    For me, just getting a preference value programmatically would be enough. I am not interested in setting them.

    Thanks,
    Robin

  12. François June 2, 2023 at 17:51 Reply

    more than 10 years later it helps me after struggling few hours, so thanks you very much !

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top