Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Another Command Window text color hack

November 6, 2013 6 Comments

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

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.
<editorial>
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…
</editorial>
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.

Related posts:

  1. Bold color text in the Command Window – Matlab Command Window text can be formatted *bold* since R2011b. ...
  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. Command Window text manipulation – Special control characters can be used to format text output in Matlab's Command Window. ...
  4. 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....
  5. 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....
  6. 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...
Desktop Pure Matlab
Print Print
« Previous
Next »
6 Responses
  1. Chandrakanth November 7, 2013 at 10:19 Reply

    Can we get colours while publishing to pdf/html?

  2. GeertvdWal November 12, 2013 at 09:57 Reply

    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.

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

    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,

    Geert

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

    • Yair Altman November 12, 2013 at 10:15 Reply

      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 December 3, 2013 at 19:18 Reply

    I hope this hard coded hack will work out properly.

  4. Andriy June 13, 2016 at 17:10 Reply

    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),'67890n']); end;
    for k=0:10, fprintf('abcd'); fprintf(['12345',repmat('t',1,k),'67890n']); end;

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

    Here (https://i.imgsafe.org/ebccebb279.png) 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?

    • Yair Altman June 14, 2016 at 12:14 Reply

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

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top