bio_img_matlab

The MATLAB Blog

Practical Advice for People on the Leading Edge

Enhanced output in the MATLAB Command Window in R2026b

When using MATLAB, I have always been most at home in the command window. I'm kind of old-school that way which is why the release of MATLAB's Terminal made me so excited. Give me the command window, with the editor alongside it, and I shall move the world!
As such, I was very happy when some of my fellow MathWorkers reached out to me and said 'The command window is evolving, go check it out.' So I did!

Truncated Matrix Outputs

You know that feeling when you create an enormous matrix at the command line and forget to suppress output with a semicolon?
a = rand(10000)
Millions of numbers scrolling by as you hammer CTRL+C to make it stop. Well, that simply doesn't happen any more.
To demonstrate, I resized my command window so that it could only display a small number of rows and columns and created an enormous matrix.
As you can see, MATLAB displayed only the part of the matrix that fit into the window and then gave me the option to show everything. Click on the blue, underlined link and you'll get all of the numbers scrolling by; just like the old days.
If you right click anywhere on the output you can also choose to copy it to the operating system clipboard or open in the MATLAB variables editor which is often far better than letting all of the numbers scroll by.
The number of rows and columns shown before truncation depends on the size of your command window so it always looks 'just right'. Overall this is a small quality of life enhancement that has been a source of joy for many of us who use MATLAB daily.

Markdown in the command window

The phrase 'This is a game-changer' is over used in the world of technical computing. Every new feature we want to discuss is a 'game-changer' and while this is technically true, some games are more important than others.
I think this is a big one! The functionality is provided by the new markdowndisp function that takes Markdown as input and renders it in either the command line or Live Editor.
Here's a screenshot from using it at the command window including lists, equations and formatted text.
Images are supported too! Here's my cat, Kiwi, posing in the MATLAB Command window courtesy of markdowndisp
The version of Markdown used is GitHub flavored markdown and many things are already supported. Tables, for example:
For this first release, however, there are some gaps. Emojis, for example, are not supported
markdowndisp(":disappointed:")

:disappointed:

so we need to resort to different methods
sad = char([55357 56862]);
markdowndisp("Don't be " + sad + "There's always a way")

Don't be 😞There's always a way

Please give this new function a try and let us know if its missing anything you need.

Programmatic markdown in the Live Editor

Whilst having rich text and graphics in the command window is a novelty, we've had it for years in the Live editor. Indeed, every post at The MATLAB Blog is a MATLAB Live Script that gets converted to WordPress via the matlab_live_to_wp package. That's why I get so excited when improvements come to the Live Editor, it directly improves one of my daily workflows.
markdowndisp brings the ability to generate rich output programmatically which will allow for more expressive, automatically generated live scripts.
rng default
tic;a=rand(10000);defaultRandTime=toc;
rng xoshiro
tic;a=rand(10000);xoshiroRandTime=toc;
rng pcg
tic;a=rand(10000);pcgRandTime=toc;
markdowndisp("## Faster Random Numbers"+ newline + "For example, when talking about how much faster R2026b's **new random number generators** " + ...
" are compared to MATLAB's default. I can include the computations *directly* in the markdown prose:" + newline + ...
"* xoshiro is " + defaultRandTime/xoshiroRandTime + "x faster" + newline + ...
"* pcg is " + defaultRandTime/pcgRandTime + "x faster" + newline + newline + ...
" on my ~~machine~~ system for large matrices. Your milage may vary and " + ...
"you should look at the [compare_rng](https://www.mathworks.com/matlabcentral/fileexchange/181947-comparerng?s_tid=prof_contriblnk) package " + ...
"to benchmark them all on your system")

Faster Random Numbers

For example, when talking about how much faster R2026b's new random number generators are compared to MATLAB's default. I can include the computations directly in the markdown prose:

  • xoshiro is 4.251x faster
  • pcg is 4.7457x faster

on my machine system for large matrices. Your milage may vary and you should look at the compare_rng package to benchmark them all on your system

Combine markdowndisp with a few Live Controls and I can now create entire, interactive, rich text computational documents to send to my non-programmer friends. Hide all the code, share via MATLAB Online and they won't need to see a single line of code or even install MATLAB to use my interactive analysis.
This really does change the game!

Styled and coloured text in the MATLAB command window using ANSI escape codes

Yair Altman's superb cprintf command on the File Exchange gives an indication of how popular the idea of formatting text in the command window is. This next update is a step towards providing such support natively.
We now make use of ANSI Control Sequences to do things like this
The syntax looks rather arcane, and it is, but its also completely standard. Let's take a closer look at the string in the command
fprintf("\x1b[1;32m Bold green text\x1b[0m\n")
referring to ANSI escape code - Wikipedia as we go
  • \x1b The ESC byte. Essentially this says "The following bytes are not data, they are a command"
  • [ This is the Control Sequence Introducer
  • 1;32 These are the arguments to our command. 1 means 'bold' and 32 means 'Green foreground'.
  • m This is the final byte, the opcode. Essentially this is the function we want to run and refers to 'Select Graphics Rendition'
Then we have the string we wish to display. Finally, we have another escape sequence \x1b[0m which means 'Reset back to normal'
Here's a list of all of the standard foreground colors and their ANSI codes
names = ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white"];
for k = 0:7
fprintf("\x1b[%dm %d %s\x1b[0m\n", 30+k, 30+k, names(k+1))
end
I know what you're thinking. 37 is grey and not white. It turns out that this is standard too! Different terminals will render 'white' in different ways (see the ANSI escape codes Wikipedia page for a demonstration) but it's almost always some form of light grey. The reason is because there are also "bright" versions of each of these colours. If you want honest-to-goodness "white" you have to ask for "bright white". Since all of the "bright" colors start at code 90 ("bright black!"), "bright white" is code 97.
Background colors are in the 40s. So, 40 gives a black background, 41 is a red background and so on. Here's bright white (code 97) on a black background (code 40).
fprintf("\x1b[97;40mThis is bright white on black\x1b[0m\n")
Here's a cribsheet for the standard color codes
k = (0:7)';
t = table(30+k, 90+k, 40+k, 100+k, ...
VariableNames=["foreground" "fgBright" "background" "bgBright"], ...
RowNames=names)
t = 8×4 table
foregroundfgBrightbackgroundbgBright
1 black309040100
2 red319141101
3 green329242102
4 yellow339343103
5 blue349444104
6 magenta359545105
7 cyan369646106
8 white379747107
Combine these with a few extra modifiers such as the following and you should be good to get started
attrs = ["bold" "italic" "underline" "strikethrough"];
t = table([1 3 4 9]', [22 23 24 29]', ...
VariableNames=["on" "off"], RowNames=attrs)
t = 4×2 table
onoff
1 bold122
2 italic323
3 underline424
4 strikethrough929
Let's do bright red on black with strikethrough
fprintf("\x1b[9;91;40mBright red on black with strikethrough\x1b[0m\n")
Finally, code 5 is 'blink' but it's unsupported. We're not monsters!

Interesting side effects and edge cases

I wondered what would happen if I didn't close a string with \x1b[0m. It turns out that the output of the command window is changed for all subsequent output
Could have a lot of fun by changing someone's command window to bright white while they were away from their desk! So now, all my output is bold and red until I reset it with a fprintf("\x1b[0m")
Not everything is affected though. The new markdowndisp function, for example, does not support ANSI codes although the venerable disp function does
When MATLAB returns a matrix, that is not affected either
Strings are though
HTML for bold and hyperlinks is not supported when using ANSI codes. As such, Table and other outputs containing bold or hyperlinks will show their underlying HTML when used like this. For example:
The escape characters don't work at all in live scripts yet
There might be more such edge cases. Let me know if you find any

How to turn off markdown display and truncated matrices for the command window

Whenever a change is made to a fundamental part of MATLAB, some people will always want to go back to the way things were. You cannot turn off support for ANSI escape codes but you can turn off the rendering of markdown via markdowndisp and truncated matrices.
In the MATLAB Desktop, Click on Settings in the Environment section. Select MATLAB>Command Window, and then clear the Show enhanced output (truncate matrix, render Markdown) option.
Matrices won't be shown truncated anymore and markdowndisp will behave like disp in the command window

Over to you

Please give all of this new functionality a try, let us know what you think and what you think we should do next in this area. For more of what's new in R2026b, check out my tour of the new release.
|
  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。