4.8

4.8 | 10 ratings Rate this file 287 downloads (last 30 days) File Size: 6.29 KB File ID: #24093

cprintf - display formatted colored text in the Command Window

by Yair Altman

 

12 May 2009 (Updated 28 Sep 2009)

Code covered by BSD License  

Displays sprintf-formatted strings in the Command Window using the specified color/underline style

Download Now | Watch this File

File Information
Description

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: 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
 
        'Black','Cyan','Magenta','Blue','Green','Red','Yellow','White'
 
Note: styles beginning with '-' will be underlined. For example:
   '-Blue' => underlined blue, like 'Hyperlinks'
   '-Comments' => underlined green
 
STYLE also accepts a regular Matlab RGB vector, that can be negated for underlining. For example: -[0,1,1] means underlined cyan.
 
STYLE is case-insensitive and accepts unique partial strings just like handle property names.
 
Examples (see results in the attached screenshot):
     cprintf('text', 'regular black text');
     cprintf('hyper', 'followed %s','by');
     cprintf('k', '%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');
 
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: http://UndocumentedMatlab.com/blog/cprintf/

See details & current limitations in the main help section

MATLAB release MATLAB 7.5 (R2007b)
Other requirements tested on Matlab 7.1, 7.5 & 7.6 (with a few version-specific quirks detailed in the help section). Should work on all Matlab 7 versions, but I haven't tested this.
Zip File Content  
Other Files cprintf.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (15)
13 May 2009 Albert

Great, thanks!

13 May 2009 Omid Khanmohamadi

A while ago, a colleague asked me if it was possible to display in a color other than the default, in the Command Window, the values violating some thresholds in an optimization problem. My response was, "That must be doable, because MATLAB does that for, say, error messages. I will look into it." Well, you looked into it alright, Yair!

14 May 2009 Aurelien Queffurust

Tested successfully with the current release R2009a under Windows and with R2008b under Linux 64-bits. Just a great submission!

15 May 2009 Petter

I did not know this was possible

27 May 2009 Petter

The function prints an extra white space after each string. Example:

str = 'Welcome!';
n = length(str);
for k = 1:n
    col = [k/n 0 0];
    cprintf(col,str(k));
end
fprintf('\n');

results in:
 W e l c o m e !

27 May 2009 Petter

OK, I now think that may be required.

27 May 2009 Yair Altman

yes Petter - this is a known limitation of CPRINTF that is mentioned in its help section as well as the TODO list (at the bottom of the file). Unfortunately, it seems that if there is no space separator, then Matlab attempts to combine the adjacent style segments, thereby overrunning the first segment's style with the second's. Adding a separator solves this. If anyone finds a workaround I'll be happy to hear. Until then it's a small price to pay...

03 Jun 2009 Alan B  
04 Jun 2009 Karl

This is great! I wish Matlab documented its Java innards better. Anyway, there seems to be another small bug.

fprintf('Here: ')
cprintf('blue', 'Blue\n');

results in a blue underscore followed by the word 'Blue' in white (invisible on white background of my command window - but shows up when highlighted).

However, the same code as above, only using '-blue' instead of 'blue', works fine (gives the expected underlined blue text).

I'm using Matlab 2007b on a Linux box.

04 Jun 2009 Yair Altman

@Karl - this is a corollary of the known issue with non-newline-terminated strings. In this case, the initial fprintf() was non-newline-terminated, getting cprintf confused...

A workaround for this case is to simply use cprintf for the initial black text, instead of fprintf:

cprintf('text', 'Here: ');
cprintf('blue', 'Blue\n');

18 Jun 2009 Mike

There seems to be another bug, this one involving the '%' character. For example,

cprintf( 'keyword', '%% keyword\n' )

merely turns the command prompt (double chevron) blue, rather than printing '% keyword' and a newline character.

fprintf( '%% keyword\n' ), on the other hand, prints just what you would expect.

18 Jun 2009 Mike  
29 Sep 2009 Florian Frank

This is a great little tool! Thank You!

03 Nov 2009 A Millo  
17 Nov 2009 bocovp  
Please login to add a comment or rating.
Updates
28 Sep 2009

Fixed some problems reported by Andreas G & Swagat K

28 Sep 2009

Fixed some problems reported by Andreas G, Swagat K

Tag Activity for this File
Tag Applied By Date/Time
java Yair Altman 13 May 2009 09:23:03
desktop Yair Altman 13 May 2009 09:23:03
gui Yair Altman 13 May 2009 09:23:03
undocumented Yair Altman 13 May 2009 09:23:03
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com