Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Changing preferences from command line

Subject: Changing preferences from command line

From: First Name Last Name

Date: 15 Aug, 2007 18:49:22

Message: 1 of 13

any way to change/view particular preferences via command
line? Such as multithreading support on/off in R2007a?

Thanks ....

Subject: Changing preferences from command line

From: Titus

Date: 17 Aug, 2007 11:35:22

Message: 2 of 13


"First Name Last Name" <nospam@nospamplease.com> schrieb im Newsbeitrag
news:f9vhri$orl$1@fred.mathworks.com...
> any way to change/view particular preferences via command
> line? Such as multithreading support on/off in R2007a?
>
> Thanks ....

Hi,
for the multithreading: there are (undocumented) functions called
getNumberOfComputationalThreads and setNumberOfComputationalThreads, take a
look at the demo:
>> edit multithreadedcomputations

For other preferences: probably Yair could come up with some java call, I
don't know of some "official" way...

Titus

Subject: Changing preferences from command line

From: Yair Altman

Date: 17 Aug, 2007 13:43:18

Message: 3 of 13

"Titus" <titus.edelhofer@mathworks.de> wrote in message
<fa415r$kk8$1@fred.mathworks.com>...
> For other preferences: probably Yair could come up with
some > java call, I don't know of some "official" way...


Thanks for the compliment - I couldn't resist the challenge...

* The preferences are stored in the matlab.prf text file
that's stored in the user's Matlab preferences folder
(prefdir is a built-in Matlab function):

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

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

<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' => int32
  - 'R' => rectangular area
  - '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);

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

For example:
com.mathworks.services.Prefs.getBooleanPref('LayoutSnapToGrid')
com.mathworks.services.Prefs.getIntegerPref('GeneralNumComputationalThreads')

adding a second argument to get<type>Pref() appears to
indicate a default value that is returned if <pref-name> is
invalid:
com.mathworks.services.Prefs.getIntegerPref('xxxx',123)
=> 123

* STRONG WARNING: all this relies on a lot of guesswork,
trial-&-error and some common sense. It's absolutely
undocumented, unsupported and discouraged by MathWorks.
You're pretty safe as long as you stick to read-only mode.
But if you try modifying system values this way, well it
might blow in your face if you try it at home or on a system
even slightly different than the one I have (WinXP, 2007a).
Don't come crying if your Matlab prefs get all screwed up...

Yair Altman
http://ymasoftware.com

Subject: Changing preferences from command line

From: Yair Altman

Date: 17 Aug, 2007 13:53:35

Message: 4 of 13

> <pref-type> appears to be one of these:
> - 'J' => int32
>
com.mathworks.services.Prefs.set<type>Pref(<pref-name>,newValue);
> <type>: one of Boolean, Color, RGBColor, Font, Integer,
> Rectangle, String, Double (I believe Doubles get converted
> to int32 - possibly a bitwise casting since both use 32 bits)

Small correction: doubles get stored in 8 bytes (=64 bits)
so either the 'J' <pref-type> is actually an int64 (not
int32), or some other conversion takes place for doubles
that I am not aware of.

Yair Altman
http://ymasoftware.com

Subject: Changing preferences from command line

From: Ben Steiner

Date: 14 Aug, 2008 02:03:02

Message: 5 of 13

Yair
Many, many thanks for this clear & concise explanation. It
was exactly what I was looking for. Im running two instances
of Matlab and using it to set a different background color
in each. Using these java calls on the preferences has got
me tantalisingly close to a solution ... maybe I could ask
your expert advice to get to completion?

This is the code im using:
% dont use system color
com.mathworks.services.Prefs.setBooleanPref('ColorsUseSystem',0);
% use specified color
com.mathworks.services.Prefs.setColorPref('ColorsBackground',<whatever>)

where <whatever> is a java.awt.Color object returned by
com.mathworks.services.Prefs.getColorPref('ColorsBackground')

My problem now is that the new color (or preference) does
not take effect on the desktop immediately. Instead, it
comes into effect either after a restart of Matlab or if I
open the Preferences dialog manually and then push 'Apply'.

So a quick 'n dirty solution is to use:

preferences('Colors') this opens the dialog box and then
requires the user to push 'ok' or 'Apply'. It gets the job
done, but not as elegantly as I would like.

I would like to either
1) Call the code behind the Apply button in the Preferences
dialog; or
2) Force matlab to pick up the new preferences as it would
at startup; or
3) 'refresh' the desktop

If anyone can shed light on any of these approaches I would
be most grateful. Im using R2007b,
many thanks
Ben

ps: For anyone else that's playing with this: I dont 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.

Subject: Changing preferences from command line

From: Yair Altman

Date: 19 Mar, 2009 01:37:02

Message: 6 of 13

"Ben Steiner" <nospam@nospam.com> wrote in message <g803om$4e2$1@fred.mathworks.com>...
> Yair
> Many, many thanks for this clear & concise explanation. It
> was exactly what I was looking for. Im running two instances
> of Matlab and using it to set a different background color
> in each. Using these java calls on the preferences has got
> me tantalisingly close to a solution ... maybe I could ask
> your expert advice to get to completion?
>
> This is the code im using:
> % dont use system color
> com.mathworks.services.Prefs.setBooleanPref('ColorsUseSystem',0);
> % use specified color
> com.mathworks.services.Prefs.setColorPref('ColorsBackground',<whatever>)
>
> where <whatever> is a java.awt.Color object returned by
> com.mathworks.services.Prefs.getColorPref('ColorsBackground')
>
> My problem now is that the new color (or preference) does
> not take effect on the desktop immediately. Instead, it
> comes into effect either after a restart of Matlab or if I
> open the Preferences dialog manually and then push 'Apply'.
...

I found a solution and so I'm posting it here for those interested:

After modifying the preferences, we need to notify all the components who use them to update themselves. This is standard practice in Java. In short, the relevant functions to call are these (for foreground & background colors):

com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsText');
com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsBackground');

However, if you only wish to set the colors in the command window and not in all the other Matlab text panes, then forget the prefs because they affect all the Matlab text panes at once. Instead, simply use the following short code snippet (you may need to tweak it for particular Matlab versions):

listeners=com.mathworks.mde.cmdwin.CmdWinDocument.getInstance.getDocumentListeners;
jTextArea = listeners(3);
% note: a safer way would be to loop on listeners until finding a JTextArea instance

Now you can set the colors (which apply immediately) using several alternative ways:
jTextArea.setBackground(java.awt.Color.yellow);
jTextArea.setBackground(java.awt.Color(1,1,0));
set(jTextArea,'Background','yellow');
set(jTextArea,'Background',[1,1,0]);

You can do the same with the Foreground property.

Yair Altman
http://undocumented-matlab.com

Subject: Changing preferences from command line

From: Ben Steiner

Date: 20 Mar, 2009 21:24:01

Message: 7 of 13

Yair

Thank you Sir!
 
Your first suggestion
...com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsBackground')...
does the job perfectly.

Thank you for taking the time to dwell on this and then post your solution.

With gratitude,
Ben

Subject: Changing preferences from command line

From: Diederick

Date: 19 Nov, 2009 03:02:03

Message: 8 of 13

Hi,

I'm pretty happy with the below "undocumented" functionality. Got myself a nice m-file now can run to get a new desktop looking like i want it too (i prefer light text on a dark background for instance). However, it seems not all preferences are actually in the matlab.prf file. Specifically, I am trying to do settings for cell highlighting, those two checkboxes and one color do not seem to be in the preferences file. I'm on R2009b, but I suppose any older version wouldn't have them too.

Does anybody know where to find them / if it is possible to set them by commands?

If anybody is interested in my configuration script, I can paste it in on request.

Best and thanks!
Dee

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <fa48lm$834$1@fred.mathworks.com>...
> "Titus" <titus.edelhofer@mathworks.de> wrote in message
> <fa415r$kk8$1@fred.mathworks.com>...
> > For other preferences: probably Yair could come up with
> some > java call, I don't know of some "official" way...
>
>
> Thanks for the compliment - I couldn't resist the challenge...
>
> * The preferences are stored in the matlab.prf text file
> that's stored in the user's Matlab preferences folder
> (prefdir is a built-in Matlab function):
>
> edit(fullfile(prefdir,'matlab.prf'));
>
> Each preference appears to be on a separate line in the
> following format: <pref-name>=<pref-type><pref-value>
>
> <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' => int32
> - 'R' => rectangular area
> - '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);
>
> <type>: one of Boolean, Color, RGBColor, Font, Integer,
> Rectangle, String, Double (I believe Doubles get converted
> to int32 - possibly a bitwise casting since both use 32 bits)
>
> For example:
> com.mathworks.services.Prefs.getBooleanPref('LayoutSnapToGrid')
> com.mathworks.services.Prefs.getIntegerPref('GeneralNumComputationalThreads')
>
> adding a second argument to get<type>Pref() appears to
> indicate a default value that is returned if <pref-name> is
> invalid:
> com.mathworks.services.Prefs.getIntegerPref('xxxx',123)
> => 123
>
> * STRONG WARNING: all this relies on a lot of guesswork,
> trial-&-error and some common sense. It's absolutely
> undocumented, unsupported and discouraged by MathWorks.
> You're pretty safe as long as you stick to read-only mode.
> But if you try modifying system values this way, well it
> might blow in your face if you try it at home or on a system
> even slightly different than the one I have (WinXP, 2007a).
> Don't come crying if your Matlab prefs get all screwed up...
>
> Yair Altman
> http://ymasoftware.com
>

Subject: Changing preferences from command line

From: Yair Altman

Date: 19 Nov, 2009 14:03:19

Message: 9 of 13

"Diederick " <dcniehorsterDEL@hkuDEL.hkDEL> wrote in message <he2cfb$42d$1@fred.mathworks.com>...
> Hi,
>
> I'm pretty happy with the below "undocumented" functionality. Got myself a nice m-file now can run to get a new desktop looking like i want it too (i prefer light text on a dark background for instance). However, it seems not all preferences are actually in the matlab.prf file. Specifically, I am trying to do settings for cell highlighting, those two checkboxes and one color do not seem to be in the preferences file. I'm on R2009b, but I suppose any older version wouldn't have them too.
>
> Does anybody know where to find them / if it is possible to set them by commands?
>
> If anybody is interested in my configuration script, I can paste it in on request.
>
> Best and thanks!
> Dee

Of course they are in matlab.prf:
EditorCodepadHighVisible=Btrue
Editorhighlight-lines=C-197412
EditorCodeBlockDividers=Bfalse

In the future, all you have to do to get the name of a specific preference, is to save a version of matlab.prf before and after updating the preferences, then use any diff tool (even Matlab's built-in diff tool) to immediately get the preference name(s).

Yair Altman
http://UndocumentedMatlab.com

Subject: Changing preferences from command line

From: Diederick

Date: 20 Nov, 2009 00:18:03

Message: 10 of 13

Hi Yair,

Somehow I didn't recognize those as being the ones.
Thanks for the diff trick and the help!
Dee

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <he3j77$3j1$1@fred.mathworks.com>...
> "Diederick " <dcniehorsterDEL@hkuDEL.hkDEL> wrote in message <he2cfb$42d$1@fred.mathworks.com>...
> > Hi,
> >
> > I'm pretty happy with the below "undocumented" functionality. Got myself a nice m-file now can run to get a new desktop looking like i want it too (i prefer light text on a dark background for instance). However, it seems not all preferences are actually in the matlab.prf file. Specifically, I am trying to do settings for cell highlighting, those two checkboxes and one color do not seem to be in the preferences file. I'm on R2009b, but I suppose any older version wouldn't have them too.
> >
> > Does anybody know where to find them / if it is possible to set them by commands?
> >
> > If anybody is interested in my configuration script, I can paste it in on request.
> >
> > Best and thanks!
> > Dee
>
> Of course they are in matlab.prf:
> EditorCodepadHighVisible=Btrue
> Editorhighlight-lines=C-197412
> EditorCodeBlockDividers=Bfalse
>
> In the future, all you have to do to get the name of a specific preference, is to save a version of matlab.prf before and after updating the preferences, then use any diff tool (even Matlab's built-in diff tool) to immediately get the preference name(s).
>
> Yair Altman
> http://UndocumentedMatlab.com

Subject: Changing preferences from command line

From: Manfredo Atzori

Date: 18 May, 2011 13:07:03

Message: 11 of 13

Dear all,

First of all thanks Yair for the great work.
I have the same problem of Ben, but with"refreshing" preferences in order to change a StringPref parameter.
In particular, I am doing:
com.mathworks.services.Prefs.setStringPref('HTMLProxyHost','XX.XXX.XX.XX');
com.mathworks.services.Prefs.setStringPref('HTMLProxyPort','X');

The proxy preference will change after Matlab is restarted, while I would like it to change immediately.
Do you have any suggestion?

Thanks a lot!
Manfredo


"Yair Altman" wrote in message <gps7ju$7vl$1@fred.mathworks.com>...
> "Ben Steiner" <nospam@nospam.com> wrote in message <g803om$4e2$1@fred.mathworks.com>...
> > Yair
> > Many, many thanks for this clear & concise explanation. It
> > was exactly what I was looking for. Im running two instances
> > of Matlab and using it to set a different background color
> > in each. Using these java calls on the preferences has got
> > me tantalisingly close to a solution ... maybe I could ask
> > your expert advice to get to completion?
> >
> > This is the code im using:
> > % dont use system color
> > com.mathworks.services.Prefs.setBooleanPref('ColorsUseSystem',0);
> > % use specified color
> > com.mathworks.services.Prefs.setColorPref('ColorsBackground',<whatever>)
> >
> > where <whatever> is a java.awt.Color object returned by
> > com.mathworks.services.Prefs.getColorPref('ColorsBackground')
> >
> > My problem now is that the new color (or preference) does
> > not take effect on the desktop immediately. Instead, it
> > comes into effect either after a restart of Matlab or if I
> > open the Preferences dialog manually and then push 'Apply'.
> ...
>
> I found a solution and so I'm posting it here for those interested:
>
> After modifying the preferences, we need to notify all the components who use them to update themselves. This is standard practice in Java. In short, the relevant functions to call are these (for foreground & background colors):
>
> com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsText');
> com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsBackground');
>
> However, if you only wish to set the colors in the command window and not in all the other Matlab text panes, then forget the prefs because they affect all the Matlab text panes at once. Instead, simply use the following short code snippet (you may need to tweak it for particular Matlab versions):
>
> listeners=com.mathworks.mde.cmdwin.CmdWinDocument.getInstance.getDocumentListeners;
> jTextArea = listeners(3);
> % note: a safer way would be to loop on listeners until finding a JTextArea instance
>
> Now you can set the colors (which apply immediately) using several alternative ways:
> jTextArea.setBackground(java.awt.Color.yellow);
> jTextArea.setBackground(java.awt.Color(1,1,0));
> set(jTextArea,'Background','yellow');
> set(jTextArea,'Background',[1,1,0]);
>
> You can do the same with the Foreground property.
>
> Yair Altman
> http://undocumented-matlab.com

Subject: Changing preferences from command line

From: Manfredo Atzori

Date: 18 May, 2011 13:08:03

Message: 12 of 13

Dear all,

First of all thanks Yair for the great work.
I have the same problem of Ben, but with"refreshing" preferences in order to change a StringPref parameter.
In particular, I am doing:
com.mathworks.services.Prefs.setStringPref('HTMLProxyHost','XX.XXX.XX.XX');
com.mathworks.services.Prefs.setStringPref('HTMLProxyPort','X');

The proxy preference will change after Matlab is restarted, while I would like it to change immediately.
Do you have any suggestion?

Thanks a lot!
Manfredo


"Yair Altman" wrote in message <gps7ju$7vl$1@fred.mathworks.com>...
> "Ben Steiner" <nospam@nospam.com> wrote in message <g803om$4e2$1@fred.mathworks.com>...
> > Yair
> > Many, many thanks for this clear & concise explanation. It
> > was exactly what I was looking for. Im running two instances
> > of Matlab and using it to set a different background color
> > in each. Using these java calls on the preferences has got
> > me tantalisingly close to a solution ... maybe I could ask
> > your expert advice to get to completion?
> >
> > This is the code im using:
> > % dont use system color
> > com.mathworks.services.Prefs.setBooleanPref('ColorsUseSystem',0);
> > % use specified color
> > com.mathworks.services.Prefs.setColorPref('ColorsBackground',<whatever>)
> >
> > where <whatever> is a java.awt.Color object returned by
> > com.mathworks.services.Prefs.getColorPref('ColorsBackground')
> >
> > My problem now is that the new color (or preference) does
> > not take effect on the desktop immediately. Instead, it
> > comes into effect either after a restart of Matlab or if I
> > open the Preferences dialog manually and then push 'Apply'.
> ...
>
> I found a solution and so I'm posting it here for those interested:
>
> After modifying the preferences, we need to notify all the components who use them to update themselves. This is standard practice in Java. In short, the relevant functions to call are these (for foreground & background colors):
>
> com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsText');
> com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsBackground');
>
> However, if you only wish to set the colors in the command window and not in all the other Matlab text panes, then forget the prefs because they affect all the Matlab text panes at once. Instead, simply use the following short code snippet (you may need to tweak it for particular Matlab versions):
>
> listeners=com.mathworks.mde.cmdwin.CmdWinDocument.getInstance.getDocumentListeners;
> jTextArea = listeners(3);
> % note: a safer way would be to loop on listeners until finding a JTextArea instance
>
> Now you can set the colors (which apply immediately) using several alternative ways:
> jTextArea.setBackground(java.awt.Color.yellow);
> jTextArea.setBackground(java.awt.Color(1,1,0));
> set(jTextArea,'Background','yellow');
> set(jTextArea,'Background',[1,1,0]);
>
> You can do the same with the Foreground property.
>
> Yair Altman
> http://undocumented-matlab.com

Subject: Changing preferences from command line

From: Seyyed Ali

Date: 28 Jan, 2013 21:24:09

Message: 13 of 13

Dear Yair,

I used this approach to change "Wrap Lines" in command window using:

com.mathworks.services.Prefs.setBooleanPref('CommandWindowWrapLines',1)

But I don't know how to notify proper listeners to make it effective simultaneously. May you help me with that?

Thanks,
Ali


"Yair Altman" wrote in message <gps7ju$7vl$1@fred.mathworks.com>...
> "Ben Steiner" <nospam@nospam.com> wrote in message <g803om$4e2$1@fred.mathworks.com>...
> > Yair
> > Many, many thanks for this clear & concise explanation. It
> > was exactly what I was looking for. Im running two instances
> > of Matlab and using it to set a different background color
> > in each. Using these java calls on the preferences has got
> > me tantalisingly close to a solution ... maybe I could ask
> > your expert advice to get to completion?
> >
> > This is the code im using:
> > % dont use system color
> > com.mathworks.services.Prefs.setBooleanPref('ColorsUseSystem',0);
> > % use specified color
> > com.mathworks.services.Prefs.setColorPref('ColorsBackground',<whatever>)
> >
> > where <whatever> is a java.awt.Color object returned by
> > com.mathworks.services.Prefs.getColorPref('ColorsBackground')
> >
> > My problem now is that the new color (or preference) does
> > not take effect on the desktop immediately. Instead, it
> > comes into effect either after a restart of Matlab or if I
> > open the Preferences dialog manually and then push 'Apply'.
> ...
>
> I found a solution and so I'm posting it here for those interested:
>
> After modifying the preferences, we need to notify all the components who use them to update themselves. This is standard practice in Java. In short, the relevant functions to call are these (for foreground & background colors):
>
> com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsText');
> com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsBackground');
>
> However, if you only wish to set the colors in the command window and not in all the other Matlab text panes, then forget the prefs because they affect all the Matlab text panes at once. Instead, simply use the following short code snippet (you may need to tweak it for particular Matlab versions):
>
> listeners=com.mathworks.mde.cmdwin.CmdWinDocument.getInstance.getDocumentListeners;
> jTextArea = listeners(3);
> % note: a safer way would be to loop on listeners until finding a JTextArea instance
>
> Now you can set the colors (which apply immediately) using several alternative ways:
> jTextArea.setBackground(java.awt.Color.yellow);
> jTextArea.setBackground(java.awt.Color(1,1,0));
> set(jTextArea,'Background','yellow');
> set(jTextArea,'Background',[1,1,0]);
>
> You can do the same with the Foreground property.
>
> Yair Altman
> http://undocumented-matlab.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us