Yesterday I received an email asking if it was possible to programmatically change Matlab’s Command Window foreground and background colors. This is possible from the File/Preferences window, but requires interactive user actions. The question was whether it was possible to do so programmatically, in order to place the necessary commands in some script (for example, startup.m). This is important, for example, when we have two Matlab applications open at the same time and wish to visually distinguish between them.
After getting this email, one in a long list of similar questions I tackled over the past few years regarding Matlab’s undocumented/unsupported/hidden features, I decided it was time to start a blog on this broad subject. I do not know if there is any interest in the subject of this blog, so I would be extremely happy to hear feedback.
Changing Command-Window colors can be done programmatically in two manners: The first is by modifying system preferences. The issue of programmatic access to system preferences is detailed in another dedicated post. Here is the bottom line regarding the colors:
% Don't use system color com.mathworks.services.Prefs.setBooleanPref('ColorsUseSystem',0); % Use specified colors for foreground/background (instead of the default black on white) com.mathworks.services.Prefs.setColorPref('ColorsBackground',java.awt.Color.yellow); com.mathworks.services.ColorPrefs.notifyColorListeners('ColorsBackground');
and similarly for the foreground color, whose property name is called ‘ColorsText’. Note that colors can be specified in several alternative manners (see below).
This affects all Matlab text panes (Command Window, Command History, Workspace Browser etc.), for this and all future Matlab sessions. If you only wish to set the colors in the command window and not in all the other Matlab text panes, or if you only wish to modify this session, then forget the prefs method. Instead, simply use the following short code snippet (you may need to tweak it for particular Matlab versions) to change the colors of only the command text pane (that’s a Swing JTextArea, for those of you who are java-savvy).
cmdWinDoc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance; listeners = cmdWinDoc.getDocumentListeners; jTextArea = listeners(3);
Note: a safer way would be to loop on listeners until finding a JTextArea instance, since it is not assured to be the 3rd item in the listeners array.
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.
Please let me know what you think of this post, or of this blog in general. Your feedback, as well as additional questions and suggestions are most welcome.
Yair Altman
altmany at gmail dot com
Related posts:
- 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...
- 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...
- Changing system preferences programmatically Matlab user/system preferences can be changed programmatically, from within your Matlab application or from the Matlab desktop command prompt. This post details how....
- EditorMacro v2 – setting Command Window key-bindings The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....
- setPrompt – Setting the Matlab Desktop prompt The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...
- 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....
Categories: Desktop, High risk of breaking in future versions, Java
This entry was posted
on Thursday, March 19th, 2009 at 12:36 pm PST
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Yair
Thanks for addressing this point.
In response to your question of is there an interest in undocumented/unsupported/hidden matlab features? YES, yes, and yes again.
I look forward to reading and learning.
If I come across other non trivial issues I will be sure to post here as well (it might attract your attention quicker than Matlab Central?!)
take it easy
Ben
Thanks for your feedback Ben – you are my first and as yet only commentator, so at least I know I’m not writing in a vacuum…
Dear readers – the more feedback I receive, the better I will be able to tune this blog into something that is interesting to you. So, if you have any preference at all, please leave a reply or shoot me an email (altmany at gmail), about anything: the length of my posts, how detailed you want to see the code snippets, whether you’re more interested in Java internals or undocumented features in plain Matlab functions – anything at all. I’ll do my best to accommodate your wishes and make this blog as interesting as possible,
Yair
Good work. I expect your work goes more into extending the capability of the gui and interface programming; My only thought is that matlab is a child to the windows shell environment and should have inherited all the .net properties/attributes too. So your stuff is a step in the right direction.
Right now I am looking at generating (simple) dynamic html scripts using matlab guis to report collected data, don’t suppose you know of someone who’s done that already and published a collection of functions of that type. I know about the report generation feature that matlab has, but it’s customizability sucks and it does not handle all the data types very well.
Best of luck and regards
Sid
Hi Yair
I saw that memory monitor is on the TODO list.
At the risk of being selfish(!), here is another suggestion. One of my frustrations concerns the memory management. I’m using release 2007b so maybe it improved in later versions anyway. However if not, here’s a brief description:
“feature(‘memstats’)” gives me the size of the largest contiguous free block. This limits the maximum size of a variable I can use.
As I run code & create variables the largest free block will only ever decrease in size. “clear”, “clear global”, “java.lang.System.gc” will clear variables and memory but appears not to increase the size of the largest free block. It is as if the memory is lost. Eventually, i quit and restart Matlab to start with a decent sized largest free block.
So my question is (if you feel inclined) is their a nifty way to “give back” the memory, increase the size of the largest free block and avoid having to quit?
all the very best
Ben
Hello,
I found your recipe for changing the background color of the command window very helpful. I have been looking for such an instruction for quite some time because I think that a tinted screen is easier on the eyes than white.
Thank you and best wishes,
Peter Navé
2009-03-27
Ben – in response to your question: I was able to recreate your predicament and nothing I tried worked, just like you said
This is actually a great topic to investigate since it stands to reason that there’s a Java hack somewhere to pack the memory.
I’ll keep trying new ideas and if I come across something I’ll let you know.
Yair
I stumbled on matlab’s save stdio feature recently; only appears to work with -nodesktop mode?
matlab -r ‘x = rand(5,1);save stdio x;exit’ -nodesktop
[...] In earlier posts I showed how to modify the Command Window (CW) text and background color, and a very limited method of displaying red (error) and blue (hyperlinked) CW messages. [...]
[...] Following my post on Changing Matlab’s Command Window colors, I received an email asking whether it was possible to temporarily set the Command-Window text color [...]
[...] 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 [...]
Hi Yair.
First of all, the undocumented Matlab stuff is very helpful, and it seems like you are a wizard in this field.
In regard to this post, I have a different (yet related) inquiry.
When creating a GUI programatically, I can select my GUI’s colors using the uicontrol property “BackgroundColor” and the figure property “Color”.
However, I have encountered two annoying things:
1. I cannot set the uimenu background color (it is system set)
2. When creating a slider uicontrol, I can set the background color, but not the slider button itself and the arrow up/down buttons.
Wanted to know whether you know how to do this.
Thanks in advance, and keep doing this unusual and great service for the Matlab community.
Tal.
Thanks Tal
1. uimenu background, font, icon, alignment etc. can be set by accessing the Java handle of the Matlab object. I plan to do an extensive post on this issue sometime soon. In the meantime, use my FindJObj utility on the Matlab File Exchange to view/get this handle:
2. See my previous paragraph vis-a-vis FindJObj. In practice though, this is more difficult: the arrows are pre-configured (painted in runtime on top of two small button sub-components); I don’t know how to modify the slider button color – search the web for “change JScrollBar color”, since Matlab sliders are basically simple Java JScrollBar objects.
Yair
Hi Yair.
Thanks for your prompt reply.
As I am working with Matlab 7.0.4 (my fault, I know…), findjobj() is encountering all kinds of compatibility problems. I might try to debug it sometime.
Anyway, I will be looking forward for your next posts.
Is it possible to set background transparency?
Cameron – I don’t think you can set the bg transparency
Could you provide code underline text in matlab?
You can underline Matlab Command-Window text using the CPRINTF utility, as described here: http://undocumentedmatlab.com/blog/cprintf
Hi, Thanks for this code!
It works, except when I use the %% to divide parts of an m-file. The part that I want to modify still has a mother-of-pearl colored background ( I’d prefer it to be black.)
How do I change this??
Thanks again.
@Mimi – I don’t understand. Please post the code that you are using.
Is it possible to have MATLAB change the whole Desktop color scheme (so instead of these XP-colors on Linux, colors that are more like MATLAB on Mac?)
I can’t seem to wrap my head around the needed Java methods to actually change the general UI.
[...] A couple of days ago, a reader of this blog posted a comment, asking whether it is possible to change Matlab’s Desktop color scheme, and its general UI [...]
@Egon – see here