Another Command Window text color hack

Matlab’s Command Window text is notorious for its boring black & white appearance when sending output via the built-in fprintf or disp commands. Back in 2009, I explained the undocumented ability to display red or hyperlinked text using fprintf, or colored and underlined text using my cprintf utility. I followed this up last year with the relatively new ability to display bold text formatting, using either fprintf or cprintf.

A short while ago, Carsten Allefeld (a regular on this blog) alerted me to the fact that recent Matlab releases have added yet another undocumented feature, this time the ability to specify orange formatting to the output text. This made sense: after all, recent releases have started to display warnings as orange text on STDOUT. While errors have for ages been rendered red by the simple act of directing their output to STDERR, warnings are output to STDOUT and so a bypass was naturally devised. This is similar to the <strong> bypass that I explained last year, for the bold formatting. By redirecting STDOUT, Carsten was able to see that by adding '[\b...]\b' to the output text, anything between the brackets was given an orange color. Of course, we can still use cprintf to display any color in the rainbow, orange included:

Orange color in Command Window text

Orange color in Command Window text

The \b hack relies on the fact that \b is actually the standard back-space control-character (BS, or ASCII 8), which erases the preceding [ (bracket) character. Matlab’s implementation of fprintf simply has a back-door that switches to orange color when it sees a [\b – ]\b couple. By the way, it won’t work if you use any other combination, or if there are not exactly two such in the displayed text. MathWorks made the hack very specific so that it would be close to impossible to come by it by mistake.

Since ‘\b’ translates into the BS character, we can directly use char(8), as follows:

disp(['this is [' 8 'orange]' 8 ' text'])

The [\b sequence is reminiscent of the CSI ANSI sequence used in days of yore, on non-graphic consoles. In those old days, we used the ESC-[ sequence to add text formatting to the console text. Perhaps some MathWorker was nostalgic. Note the related FEX utility tcprintf, which implements ANSI sequences for color-coding the Matlab console output.

If you ask me, this is an example of bad design: instead of modifying fprintf to enable generic color and text formatting, the developer hard-coded a specific [\b hack for a specific color, and a different specific <strong> hack for bold formatting. If tomorrow a green color or underlined text will be needed, they’ll need to modify the hard-coding and extend it with yet new hard-coding. Moreover, the [\b hack does not enable multiple segments of orange colors in the same text, a natural extension. In their shoes, I’d probably have extended the <strong> paradigm with something like <font color="orange"> or: <span style="color:#FF8000;"> (I wonder where I got this idea from? hmm…) Maybe I’m just being too harsh: it’s obviously easier to bitch from the benches than to score at the field…

Anyway, if you only need black, red and orange colors in your text, you need look no further than fprintf. If, however, you require your outputs to be a bit more lively, then consider using cprintf:

cpintf demo

cpintf demo

Do you know of any additional undocumented Command Window hack? If so, please do leave a comment below.

Categories: Desktop, High risk of breaking in future versions, Stock Matlab function

Tags: ,

Bookmark and SharePrint Print

6 Responses to Another Command Window text color hack

  1. Can we get colours while publishing to pdf/html?

  2. GeertvdWal says:

    Hello Yair,

    From R2011b and up (to my knowledge at least), MATLAB error messages include a bold hyperlink to relevant documentation. Try for yourself:

    >> error
    Error using <b>error</b>
    Not enough input arguments.

    If MATLAB can do this, so can we! Some mucking about with HTML resulted in the following semi-accidental discovery:

    fprintf(1,'<a href="%s" style="font-weight:bold">%s</a>\n',...
        'matlab:%% do stuff',...
        'bold blue hyperlink'...

    This also combines with the ‘{\b … }\b’ method for printing orange text, or the directing the output to STDERR method for printing red text. It’s a bit clunky, but it works…

    Unfortunately, it seems that this is another case of hard-coded syntax trapping, which means that it doesn’t respond to other HTML tricks. Anyway, have fun with this!

    Kind regards,


    PS. thanks a million for this blog; It has helped me out on many occasions! I’m glad I can make this little contribution.

    • Yet another hard-coded hack. Of course all the regular CSS attributes like color or whatever don’t seem to work. Why couldn’t MathWorks stay consistent?! this is really beyond my understanding.

  3. Marie Kris says:

    I hope this hard coded hack will work out properly.

  4. Andriy says:

    Hello Yair,

    Perhaps, my comment does not completely matches topic of this post, but it might be interested to those who runs MATLAB with -nodesktop option. In this case MATLAB handles backspace characters in even more unusual way. Just try to rung the following commands in command prompt:

    for k=0:10, fprintf('abcd'); fprintf(['12345',repmat('\b',1,k),'67890\n']); end;
    for k=0:10, fprintf('abcd'); fprintf(['12345',repmat('\t',1,k),'67890\n']); end;

    Here ( is what I see in “-nodesktop” mode in R2015b.
    Tabs are handled properly while back-spaces can not erase text printed by previous fprintf command and produce weird results.

    Do you have any ideas how to send “\b” character properly to the command window in “-nodesktop” mode?

    • @Andriy – When you run Matlab with -desktop, you see a native (not Java) command window console, and this console does not interpret the \b as a valid control character. \t is much more widely accepted than \b, so this is not surprising. I think it’s also not recognized in Windows DOS consoles. If you want to use a lightweight Matlab console, this naturally has a much more limited feature set.

Leave a Reply

Your email address will not be published. Required fields are marked *