cprintf()
cprintf(format, v1, ... , vn)
cprintf(style, format, v1, ... , vn)
R = cprintf(style, format, v1, ... , vn)
| Parameter | Description |
|---|---|
| style | a text style name, RGB vector, or hexadecimal color. |
| format | a string describing the text format. |
| v1, ... , vn | data to convert and print according to the format parameter. |
| Parameter | Description |
|---|---|
| R | number of visible characters written to stdout. |
cprintf writes formatted text to stdout and applies a text style when the current interface supports styled output. The basic CLI displays plain text; advanced CLI and GUI can render styles.
Without a style argument, cprintf behaves like a stdout formatted print using plain text. With no input argument, it displays a short style demo.
The format argument follows the same formatting rules as fprintf.
Named styles include text, keywords, comments, strings, unterminatedstrings, systemcommands, errors, hyperlinks, and common color names such as black, blue, cyan, green, magenta, red, yellow, white, gray, orange, pink, purple, brown, and gold.
Named styles and colors accept unambiguous prefixes. Light and dark variants can be written with a light or dark prefix, for example lightblue or dark-green.
RGB colors can be specified as a three-element vector in the range [0, 1] or [0, 255]. Hexadecimal colors can use #RGB or #RRGGBB.
A leading * requests bold text. A leading _ or - requests underlined text. Prefixes can be combined in either order, for example *_red or _*#0af.
Limitations: rendering depends on the terminal or GUI capabilities; the basic CLI and interfaces without style support display plain text. evalc captures only visible text. Files and diary output do not preserve style information. Output is always sent to stdout, never to stderr.
cprintf()
cprintf('value: %.3f\n', pi)
cprintf('red', 'red text\n')
cprintf('blue', 'blue text\n')
cprintf('cy', 'cyan from an unambiguous prefix\n')
cprintf('lightgreen', 'light green\n')
cprintf('dark-blue', 'dark blue\n')
cprintf([1 0.4 0], 'normalized RGB\n')
cprintf([0 128 255], 'byte RGB\n')
cprintf('#0af', 'short hex\n')
cprintf('#00aaff', 'long hex\n')
cprintf('*red', 'bold red\n')
cprintf('_green', 'underlined green\n')
cprintf('*_dark-magenta', 'bold underlined dark magenta\n')
msg = ['accent ', char(233)];
count = cprintf('comments', 'line 1\n%s\n', msg)
txt = evalc('cprintf(''red'', ''captured'')')
| Version | Description |
|---|---|
| 2.0.0 | initial version |