Changing Matlab’s Command Window colors - part 2

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 (CW)’s text color, so that subsequent disp or fprintf commands will be displayed in some specific text color.

The short and simple answer is no: You can change the text color as described in that post, but once you modify it, the entire CW text will be modified accordingly.

Unfortunately, CW (like Swing’s standard JTextArea of which CW is an instance) does not automatically support HTML formatting as most other uicontrols. In fact, CW’s default Document object, which holds all the text-area’s text and font style information, is an extension of Swing’s javax.swing.text.PlainDocument, which does not allow any text style formatting. And the CW object itself is a simple JTextArea, which does not enable using a styled Document object. Perhaps in a future version MathWorks would be willing to use the almost identical (syntactically-wise) JTextPane, which does enable styled text runs.

To a very limited degree, you can use the fact that hyperlinks and error messages have separate styles than standard text (respectively, blue underlined and red). So, you can output text in these styles as follows:

% Use hyperlink style:
>> disp('<a href="">my text</a>')
my text

% Use STDERR (fid=2) style:
>> fprintf(2,'my text\n')
my text

Note: you could possibly get a handle to the JTextArea (see the previous post) and then set its UI property to your tailor-made Java UI class. In this class you can set text segment properties based on whichever properties you wish. This is apparently how Matlab overcame JTextPane’s limitations: they implemented a custom com.mathworks.mde.cmdwin.CmdWinSyntaxUI class that extends javax.swing.plaf.basic.BasicTextAreaUI. But implementing such a custom UI class while retaining Matlab’s syntax-highlighting functionality is most probably very difficult and not worth the effort. So we’ll just wait for some supported method in a future Matlab release.

Addendum (May 13, 2009): I have found a way to enable display of text in any color, as well as underline, in the Command Window. See my post on cprintf for details.

Bookmark and Share

Related posts:

  1. 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....
  2. 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...
  3. 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....
  4. 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....
  5. setPrompt - Setting the Matlab Desktop prompt The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...
  6. EditorMacro - assign a keyboard macro in the Matlab editor EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....

Tags:

PoorSo-soHelpfulVery helpfulExcellent! (1 votes, average: 5.00 out of 5)
Loading ... Loading ...
Bookmark and Share Print Print

2 Responses to “Changing Matlab’s Command Window colors - part 2”

  1. Ken Orr Ken Orr says:

    Hi Yair,

    Clever use of the tools at hand! We would definitely like to allow users to change the color of their text in the Command Window (this is a frequent request). We’re thinking about more robust highlighting mechanisms now.

    Depending on your needs, publishing could also serve as an intermediate solution, as you could embed HTML into your output, which would then be rendered by a web browser.

    -Ken

  2. [...] 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. Since then, I was obsessed with finding a better way to CW display text in any color. After lots [...]

Leave a Reply


Wrap code fragments inside <pre lang="matlab"> tags, like this:

   <pre lang="matlab">
   a = magic(3);
   sum(a)
   </pre>