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.
altmany at gmail dot com
- 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...
- Bold color text in the Command Window Matlab Command Window text can be formatted *bold* since R2011b. ...
- 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....
- 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....
- Command Window text manipulation Special control characters can be used to format text output in Matlab's Command Window. ...