cprintf - display formatted colored text in Command Window

Version 1.14 (34,7 KB) von Yair Altman
Displays sprintf-formatted strings in the Command Window using the specified color/underline style
Aktualisiert 26. Mär 2022

Lizenz anzeigen

count = cprintf(style,format,...)
CPRINTF processes the specified text using the exact same FORMAT arguments accepted by the built-in SPRINTF and FPRINTF functions.
CPRINTF then displays the text in the Command Window using the specified STYLE argument. The accepted styles are those used for Matlab's syntax highlighting (see: File / Preferences / Colors / M-file Syntax Highlighting Colors), and also user-defined colors.
The possible pre-defined STYLE names are:
'Text' - default: black
'Keywords' - default: blue
'Comments' - default: green
'Strings' - default: purple
'UnterminatedStrings' - default: dark red
'SystemCommands' - default: orange
'Errors' - default: light red
'Hyperlinks' - default: underlined blue
STYLE beginning with '-' o '_' will be underlined. For example:
'-Blue' => underlined blue, like 'Hyperlinks'
'_Comments' => underlined green
STYLE beginning with '*' will be bold (R2011b+ only). For example:
'*Blue' is bold blue;
'*Comments' is bold green etc.
Note: Matlab does not currently support both bold and underline, only one of them can be used in a single cprintf command. But of course bold and underline can be mixed by using separate commands.
STYLE colors can be specified in 3 variants:
[0.1, 0.7, 0.3] - standard Matlab RGB color format in the range 0.0-1.0
[26, 178, 76] - numeric RGB values in the range 0-255
'#1ab34d' - Hexadecimal format in the range '00'-'FF' (case insensitive). 3-digit HTML RGB format is also accepted: 'a5f' = 'aa55ff'
STYLE can be underlined by prefixing - : -[0,1,1] or '-#0FF' is underlined cyan
STYLE can be made bold by prefixing * : '*[1,0,0]' or '*#F00' is bold red
STYLE is case-insensitive and accepts unique (non-ambiguous) partial strings (for example, 'cy' instead of 'cyan').
Usage examples (see results in the attached screenshot):
cprintf; % displays the demo
cprintf('text', 'regular black text');
cprintf('hyper', 'followed %s','by');
cprintf('key', '%d colored', 4);
cprintf('-comment','& underlined');
cprintf('err', 'elements\n');
cprintf('cyan', 'cyan');
cprintf('_green', 'underlined green');
cprintf(-[1,0,1], 'underlined magenta');
cprintf('*blue', 'and *bold* (R2011b+ only)\n');
cprintf([1,0.5,0],'and multi-\nline orange\n');
Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)
This code heavily relies on undocumented and unsupported Matlab functionality. It works on Matlab 7+, but use at your own risk!
A technical description of the implementation can be found at: https://UndocumentedMatlab.com/articles/cprintf
See details & current limitations in the main help section

Zitieren als

Yair Altman (2024). cprintf - display formatted colored text in Command Window (https://www.mathworks.com/matlabcentral/fileexchange/24093-cprintf-display-formatted-colored-text-in-command-window), MATLAB Central File Exchange. Abgerufen .

Kompatibilität der MATLAB-Version
Erstellt mit R2007b
Kompatibel mit R2006a und späteren Versionen
Windows macOS Linux
Mehr zu Language Fundamentals finden Sie in Help Center und MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Veröffentlicht Versionshinweise

Fixed cases of using string (not char) inputs


Fixed cases of invalid colors (especially bad on R2021b onward)

Updated the compatibility boxes to reflect that CPRINTF does NOT work starting with R2021b (only up to R2021a). No code was changed.

Enabled specifying color in multiple ways: as #RGB or #RRGGBB (hexadecimal codes e.g. '#a0b0FF'), or [0.1,0.7,0.3], or [26,178,76]

Fix by T. Hosman for embedded hyperlinks; package as a toolbox

Fixed a few discoloration issues (some other issues still remain)

Fix: if command window isn't defined yet (startup) use standard fprintf, as suggested by John Marozas

Fix: if command window isn't defined yet (startup) use standard fprintf, as suggested by John Marozas

Graceful degradation support for deployed (compiled) and non-desktop applications; minor bug fixes

Fixes for R2012b; added bold style; accept RGB string (non-numeric) style

Fixes for R2011b; fix by Danilo (FEX comment) for non-default text colors

Performance improvement

Minor fix for R2010a/b; fixed edge case reported by Sharron; CPRINTF with no args runs the demo

Fixed some problems reported by Andreas G, Swagat K

Fixed some problems reported by Andreas G & Swagat K