A reader comment last week sent me on a short wild goose chase, that had an interesting and useful conclusion. The comment requested a way to display bold text in the Command Window, similarly to the way that we can display colored and underlined text using my cprintf utility.
Since Matlab release 7.13 (R2011b), the Matlab Command Window (CW) has the ability to display bold text. This is used, for example, when displaying the function name in the output of the help function (i.e., help(‘max’) will display the term ‘max’ in bold when displaying the help section). It therefore seemed a reasonable request to add this functionality to cprintf.
Attempt #1: use the underlying Java
I stated out investigating the relevant attributes used by the CW’s underlying Java component to display the text. It turns out that in addition to the three custom attributes used by Matlab in past releases, a fourth attribute called BoldStartTokens was added, which stored the relative position index of the bold text within each text element.
Unfortunately, this attribute contains an array of integer values, that get converted to a simple numeric array in Matlab. This means that unlike the SyntaxTokens attribute, which contains an array of Java String objects that can be modified one at a time (this is used by cprintf), the BoldStartTokens attribute value cannot be modified in a similar manner. Attempting to remove and add the attribute afresh (with modified values) failed due to a
javax.swing.text.StateInvariantError: Illegal cast to MutableAttributeSet Java exception.
So unless I find a way to update the BoldStartTokens attribute values from Matlab, this looked like a dead end.
Attempt #2: follow the Matlab trail
My second attempt was to follow the Matlab trail, to see exactly what the help function does internally to format parts of the text as bold. Starting with help‘s m-file (%matlabroot%/toolbox/matlab/helptools/help.m), and step-by-step debugging, I quickly found out that the internal function prepareHelpForDisplay parses the help text and wraps the relevant terms with an HTML
<strong> tag to make these terms appear bold.
We can easily use this information ourselves:
>> fprintf('not bold, <strong> bold </strong>\n') not bold, bold % the 'bold' term appears bolded
Matlab does have an internal limitation, of not allowing hyperlinks and bold formatting together. If both
<strong> tags are present, then only the inner tag set is parsed.
<strong> is great, but apparently if we use the
<strong> tag in cprintf, the output gets messed up – the HTML tag confuses cprintf. So I took the opportunity to update cprintf such that (1) it will not get confused by a
<strong> tag, and (2) so that we could use ‘*’ in the style part (input argument #1) to specify bold format:
>> cprintf('*blue', 'this is bold\n') this is bold % this text appears bolded-blue
The benefit of using cprintf over directly using the
<strong> tag is that it works seamlessly on Matlab release 2011a and earlier that do not support the tag (the text is simply not bolded, whereas if we had used
<strong> this tag would be displayed onscreen, which is ugly). Moreover, cprintf catches a situation of trying to use both underline and bold formats, and issues a warning. Finally, of course, ‘*’ is more compact and easier to use and maintain than ‘<strong>…</strong>’ …
Please feel free to download cprintf from the File Exchange and read its contents to see how a not-too-large utility can achieve something previously believed to be impossible. And if you find ways to make cprintf better, please do let me know (I don’t mean suggestions for *me* to work on: I mean actual improvements that *you* made to the code).
- cprintf – display formatted color text in the Command Window cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...
- Command Window text manipulation Special control characters can be used to format text output in Matlab's Command Window. ...
- Changing Matlab’s Command Window colors – part 2 The Matlab Command Window enables a limited degree of inline color customization - this post describes how to use it...
- Changing Matlab’s Command Window colors Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....
- EditorMacro v2 – setting Command Window key-bindings The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....
- Setting status-bar text The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....