- Trial Software
- Product Updates
This is machine translation
Mouseover text to see original. Click the button below to return to the English version of the page.
Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.
Translate This Page
MathWorks Machine Translation
The automated translation of this page is provided by a general purpose third party translator tool.
MathWorks does not warrant, and disclaims all liability for, the accuracy, suitability, or fitness for purpose of the translation.
Display value of variable
disp( displays the
value of variable
X without printing the variable
name. Another way to display a variable is to type its name, which
displays a leading “
X =” before the
If a variable contains an empty array,
without displaying anything.
Display Variable Values
Create a variable with numbers and another variable with text.
A = [15 150]; S = 'Hello World.';
Display the value of each variable.
Display Matrix with Column Labels
Display a matrix and label the columns as
X = rand(5,3); disp(' Corn Oats Hay')
Corn Oats Hay
0.8147 0.0975 0.1576 0.9058 0.2785 0.9706 0.1270 0.5469 0.9572 0.9134 0.9575 0.4854 0.6324 0.9649 0.8003
Display Hyperlink in Command Window
Display a link to a Web page by including HTML hyperlink
code as input to
disp. For example, display
a link to the MathWorks® Web site.
X = '<a href = "https://www.mathworks.com">MathWorks Web Site</a>'; disp(X)
MathWorks Web Site
Display Multiple Variables on Same Line
Here are three ways to display multiple variable values on the same line in the Command Window.
Concatenate multiple character vectors together using the
 operator. Convert any numeric values to characters using the
num2str function. Use
disp to display the result.
name = 'Alice'; age = 12; X = [name,' will be ',num2str(age),' this year.']; disp(X)
Alice will be 12 this year.
sprintf to create text, and then display it with
name = 'Alice'; age = 12; X = sprintf('%s will be %d this year.',name,age); disp(X)
Alice will be 12 this year.
fprintf to directly display the text without creating a variable. However, to terminate the display properly, you must end the text with the newline (
name = 'Alice'; age = 12; fprintf('%s will be %d this year.\n',name,age);
Alice will be 12 this year.
X — Input array
To display more than one array, you can use concatenation or
as shown in the example, Display Multiple Variables on Same Line .
Introduced before R2006a
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
Select web site
You can also select a web site from the following list:
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- United Kingdom (English)
- Australia (English)
- India (English)
- New Zealand (English)
- 日本 (日本語)
- 한국 (한국어)
Contact your local office
- Trial Software
- Product Updates
- Release Notes
- PDF Documentation
- MATLAB Answers
- Installation Help
- Bug Reports
- Product Requirements
- Software Downloads
Introducing Deep Learning with MATLAB
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
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 :
Do you know of any additional undocumented Command Window hack? If so, please do leave a comment below.
Tags: Desktop , Pure Matlab
6 Responses to Another Command Window text color hack
- Chandrakanth says:November 7, 2013 at 10:19 am
Can we get colours while publishing to pdf/html?Reply
- GeertvdWal says:November 12, 2013 at 9:57 am
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!
PS. thanks a million for this blog; It has helped me out on many occasions! I’m glad I can make this little contribution.Reply
- Yair Altman says:November 12, 2013 at 10:15 am
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.
- Marie Kris says:December 3, 2013 at 7:18 pm
I hope this hard coded hack will work out properly.Reply
- Andriy says:June 13, 2016 at 5:10 pm
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 ( 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?Reply
- Yair Altman says:June 14, 2016 at 12:14 pm
@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 Cancel reply
My Matlab Expo 2016 keynote presentation
- Email Yair Altman
- Subscribe to new posts (email)
- Subscribe to new posts (feed)
- Subscribe to new posts (reader)
- Subscribe to comments (feed)
- Customizing web-GUI uipanel
- Scrollable GUI panels
- Multi-threaded Mex
- Plot legend customization
- Sliders in Matlab GUI – part 2
- String/char compatibility
- Blocked wait with timeout for asynchronous events
- Speeding-up builtin Matlab functions – part 2
- Speeding-up builtin Matlab functions – part 1
- Spicing up the Matlab Editor
- Auto-scale image colors
- Adding custom properties to GUI objects
- IP address input control
- Customizing axes tick labels
- Customizing histogram plots
- 2018 (16)
- August (1)
- Customizing web-GUI uipanel
- July (4)
- June (1)
- May (2)
- April (1)
- March (1)
- February (2)
- January (4)
- August (1)
- 2017 (21)
- 2016 (36)
- 2015 (37)
- 2014 (40)
- 2013 (47)
- 2012 (50)
- 2011 (47)
- 2010 (57)
- 2009 (48)
- 2018 (16)
- Desktop (43)
- Figure window (50)
- Guest bloggers (59)
- GUI (154)
- Handle graphics (82)
- Hidden property (41)
- Icons (7)
- Java (171)
- Listeners (22)
- Memory (16)
- Mex (13)
- Presumed future risk (378)
- High risk of breaking in future versions (89)
- Low risk of breaking in future versions (158)
- Medium risk of breaking in future versions (133)
- Public presentation (6)
- Semi-documented feature (9)
- Semi-documented function (35)
- Stock Matlab function (137)
- Toolbox (9)
- UI controls (50)
- Uncategorized (13)
- Undocumented feature (204)
- Undocumented function (37)
TagsActiveX AppDesigner Callbacks COM Compiler Desktop Donn Shull Editor Figure FindJObj GUI GUIDE Handle graphics HG2 Hidden property HTML Icons Internal component Java JavaFrame JIDE JMI Listener Malcolm Lidierth MCOS Memory Menubar Mex Optical illusion Performance Profiler Pure Matlab schema schema.class schema.prop Semi-documented function Toolbar uicontrol uifigure UIInspect uitable uitools Undocumented feature Undocumented function Undocumented property
- Dan (6 hours 24 minutes ago) : Thank you so much. This works well for UITables embedded in a traditional figure but I’m having issues getting findjobj to work when the UITable is embedded in a UIFigure, it just…
- Joe Burgel (3 days 13 hours ago) : Hi Yair, Once again, saving my life! Thanks so much for what you do for all these years.
- Sarah (4 days 11 hours ago) : Thank you!
- Yaroslav (4 days 20 hours ago) : @Mark, the zoom function calls resetplotview (undocumented). It, in turn, resets all modes (X/YLimMode) to auto. That is why the listeners do not trigger. To solve the issue, link…
- Mark D. (5 days 16 hours ago) : @Yaroslav your solution works great, but the double mouseclick (of zoom out or zoom in), which puts the plot in the default optimized axes, doesn’t work or behaves the same,…
- Peter Cook (7 days 7 hours ago) : Yair, One multithreaded mex file I can’t say enough good things about is getmatvar which, in addition to being faster than MATLAB builtins (i.e. matfile objects), allows…
- Jan Afridi (8 days 18 hours ago) : Thanks for community referring. I wish this help to understand Matlab and resolve the issue that I am facing.
- David Sampson (9 days 19 hours ago) : See also uix.ScrollingPanel in GUI Layout Toolbox. This works for figures (not uifigures) from R2014b onwards.
- Yair Altman (17 days 11 hours ago) : @Royi – yes, but this is definitely not easy. Hopefully custom JS integration will be better in future Matlab versions. Perhaps I’ll post something similar to this…
- Yair Altman (17 days 11 hours ago) : @Royi – I’m not familiar with any such control within Matlab, but you may possibly find such in one of the numerous online Java libraries (there were hundreds of…
- Royi (17 days 11 hours ago) : Hi, Is there a way to import and use other JS UI libraries in MATLAB App Designer?
- Royi (17 days 12 hours ago) : Hi, Is there a way to create triple slider? Namely a slider with 3 knobs to set left, center and right value? Thank You.
- Yair Altman (19 days 17 hours ago) : @Vaishta – you have to read the Matlab documentation about how to create GUI callbacks and how to share data between GUI components/callbacks (https://www.mathworks.com/…
- vaishta (19 days 17 hours ago) : I have also tried the set(ax1,’NextPlot’,’replaceall ‘) set(ax2,’NextPlot’,’replaceall ‘) set(ax3,’NextPlot’,’replaceall ‘) in the callback after the plot() function but its of no…
Undocumented Matlab © 2009-2018 Yair Altman