A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://undocumentedmatlab.com/articles/changing-matlab-command-window-colors/ below:

Changing Matlab's Command Window colors

Changing Matlab's Command Window colors

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

% 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);  % or listeners(4) or listeners(5), depending on Matlab release

cmdWinDoc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance; listeners = cmdWinDoc.getDocumentListeners; jTextArea = listeners(3); % or listeners(4) or listeners(5), depending on Matlab release

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

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

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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4