cprintf - display formatted colored text in Command Window

Version 1.16 (32,6 ko) par Yair Altman
Displays sprintf-formatted strings in the Command Window using the specified color/underline/bold style
28,1K téléchargements
Mise à jour 5 mars 2025

Afficher la licence

Syntax:
count = cprintf(style,format,...)
Description:
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 (based on Preferences): black
'Keywords' - default (based on Preferences): blue
'Comments' - default (based on Preferences): green
'Strings' - default (based on Preferences): purple
'UnterminatedStrings' - default (based on Preferences): dark red
'SystemCommands' - default (based on Preferences): orange
'Errors' - default (based on Preferences): light red
'Hyperlinks' - default (based on Preferences): blue (underlined)
'Black','Cyan','Magenta','Blue','Green','Red','Yellow','White'
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 supports both bold and underline only in R2025a or newer; on R2024b or earlier only one of them can be used in a single cprintf command.
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'], 'system', 'err'.
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([1,0.5,0],'and multi-\nline orange\n');
cprintf('*blue', 'and *bold* (R2011b+ only)\n');
cprintf('string'); % same as fprintf('string') and cprintf('text','string')
Limitations:
1. In R2011a and earlier, a single space char is inserted at the beginning of each cprintf text segment (this is ok in R2011b+).
2. In R2011a and earlier, consecutive differently-colored multi-line CPRINTFs sometimes display incorrectly on the bottom line. As far as I could tell this is due to a Matlab bug. Examples:
cprintf('-str','under\nline'); cprintf('err','red\n'); % hidden 'red', non-hidden '_'
cprintf('str','regu\nlar'); cprintf('err','red\n'); % underline red (not purple) 'lar'
3. Sometimes, non newline ('\n')-terminated segments display unstyled (black) when the command prompt chevron ('>>') regains focus on the continuation of that line (I can't pinpoint when this happens). To fix this, simply newline-terminate all command-prompt messages.
4. In R2011b and later, the above errors appear to be fixed. However, the last character of an underlined segment is not underlined for some unknown reason (add an extra space character to make it look better)
5. In old Matlab versions (e.g., Matlab 7.1 R14), multi-line styles only affect the first line. Single-line styles work as expected. R14 also appends a single space after underlined segments.
6. Bold style is only supported on R2011b+, and before R2025a cannot also be underlined. i.e., the style can be bold or underline, but not both. On R2025a or newer, style can be both bold and underline.
7. CPRINTF is not currently supported in Matlab mobile, Live Editor, deployed, diary, and terminal (no desktop) modes; The new web-based (JavaScript) desktop is only supported in R2025a or newer. This limitation depends on internal Matlab limitations that may possibly be lifted in future Matlab releases.
Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)
Warning:
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

Citation pour cette source

Yair Altman (2025). 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. Extrait(e) le .

Compatibilité avec les versions de MATLAB
Créé avec R2007b
Compatible avec les versions R2006a et ultérieures
Plateformes compatibles
Windows macOS Linux
Catégories
En savoir plus sur Language Fundamentals dans Help Center et MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Publié le Notes de version
1.16

Output to STDERR if style is 'error', 'red' or [1,0,0] (non-Desktop modes only)

1.15

Support R2025a (new Matlab desktop)

1.14

Fixed cases of using string (not char) inputs

1.13

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

1.12.0.1

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

1.12.0.0

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]

1.11.0.0

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

1.10.0.0

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

1.9.0.0

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

1.8.0.0

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

1.7.0.0

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

1.6.0.0

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

1.5.0.0

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

1.4.0.0

Performance improvement

1.3.0.0

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

1.2.0.0

Fixed some problems reported by Andreas G, Swagat K

1.1.0.0

Fixed some problems reported by Andreas G & Swagat K

1.0.0.0