- Undocumented Matlab - https://undocumentedmatlab.com -

Another Command Window text color hack

Posted By Yair Altman On 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 [1] text using fprintf, or colored and underlined text using my cprintf utility [2]. I followed this up last year with the relatively new ability to display bold text formatting [3], 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 [4] 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 [5], 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 [2]:

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


6 Comments (Open | Close)

6 Comments To "Another Command Window text color hack"

#1 Comment By Chandrakanth On November 7, 2013 @ 10:19

Can we get colours while publishing to pdf/html?

#2 Comment By GeertvdWal On November 12, 2013 @ 09:57

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 error
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,' [12]\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.

#3 Comment By Yair Altman On November 12, 2013 @ 10:15

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.

#4 Comment By Marie Kris On December 3, 2013 @ 19:18

I hope this hard coded hack will work out properly.

#5 Comment By Andriy On June 13, 2016 @ 17:10

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 ( [13]) 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?

#6 Comment By Yair Altman On June 14, 2016 @ 12:14

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


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/another-command-window-text-color-hack

URLs in this post:

[1] red or hyperlinked: http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors-part2/

[2] cprintf utility: http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/

[3] bold text formatting: http://undocumentedmatlab.com/blog/bold-color-text-in-the-command-window/

[4] ANSI sequence: https://en.wikipedia.org/wiki/ANSI_escape_code

[5] tcprintf: http://www.mathworks.com/matlabcentral/fileexchange/38252-tcprintf-ansi-colored-output-in-terminal

[6] Bold color text in the Command Window : https://undocumentedmatlab.com/articles/bold-color-text-in-the-command-window

[7] cprintf – display formatted color text in the Command Window : https://undocumentedmatlab.com/articles/cprintf-display-formatted-color-text-in-command-window

[8] Command Window text manipulation : https://undocumentedmatlab.com/articles/command-window-text-manipulation

[9] Changing Matlab's Command Window colors : https://undocumentedmatlab.com/articles/changing-matlab-command-window-colors

[10] EditorMacro v2 – setting Command Window key-bindings : https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings

[11] Changing Matlab's Command Window colors – part 2 : https://undocumentedmatlab.com/articles/changing-matlab-command-window-colors-part2

[12] : http://undocumentedmatlab.com%s

[13] : https://i.imgsafe.org/ebccebb279.png

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.