Ken & Mike on the MATLAB Desktop

May 25th, 2009

Colorizing text output

There have been a number of requests recently asking that we support colorized output in the Command Window (here, here and here). I won’t be offering a way to colorize the Command Window content, but I will be offering an alternative way to generate colorized output for viewing in a web browser.

Some of the users I’ve talked with want to color their output in the Command Window so that they can easily pick out print-outs that indicate a problem. Imagine you had a long running program that printed various “good” diagnostics, but also occasionally printed out a message indicating that something wasn’t quite right and needed to be looked at. It would be nice to color that text in red, or some other attention grabbing color. This is the case we’ll address below.

We’ll start by writing a simple function that takes a string and wraps it in an HTML font tag. This function will also take a color, which will be used for the font tags color attribute. Because the result will be HTML , the color value can be anything supported by font’s color attribute (e.g. red, #cccccc etc.). Here’s what that function looks like:

function colorizedstring = colorizestring(color, stringtocolorize)
%colorizestring wraps the given string in html that colors that text.
    colorizedstring = [‘<font color=”‘, color, ‘”>’, stringtocolorize, ‘</font>’];
end


Now all we need to do is use the function. Start by creating an HTML file in which to put your output text. Then, run your function and write to the output file as necessary using the colorizestring function. Finally, close the file for writing and open it using the web command.

Here’s what a sample usage might look like:

%% Open an HTML file to write the output into.
filename = ‘colorizedoutput.html’;
fid = fopen(filename, ‘wt’);

%% Do the long running calculation that generates lots of output.
for i=1:100
    outputswitch = rand;
    if rand < 0.80
    fprintf(fid, colorizestring(‘black’, ‘Everything is fine.<br/>’));
    elseif rand < 0.90
        fprintf(fid, colorizestring(‘orange’, ‘There may be a problem.<br/>’));
    else
        fprintf(fid, colorizestring(‘red’, ‘There is a problem.<br/>’));
    end

    fprintf(fid, ’some string<br/>’);
end

%% Close the HTML output file.
fclose(fid);

%% Open the HTML output file in MATLAB’s web browser.
web(filename);


and the resulting output:


This won’t solve everyone’s problems, but it should help to fill the gap until we can fully support colorized output in the Command Window.

3 Responses to “Colorizing text output”

  1. Timo replied on :

    What about CPRINTF by Yair Altman?
    It can be found in the File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/24093

    This has been the File Exchange Pick of the Week on May 15th, but that blog article seems to have been removed now… Perhaps because CPRINTF uses undocumented features?

  2. Petter replied on :

    Yeah, CPRINTF works beautifully.

  3. Scott Hirsch replied on :

    For those who find CPRINTF addresses their needs well - keep in mind Yair’s warning that code relying on undocumented/unsupported functionality may be more likely to break. If you want your code to last, code defensively:

    try
      cprintf(...
    catch ME
      fprintf(...
    end
    

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Ken & Mike work on the MATLAB Desktop team.
  • DP: Hi i have a problem with ezplot3, i want to plot more than i curve in the same graph but hold on command...
  • Ken: Hi Arsalan, Unfortunately there is no way to get the new Editor API in older versions of MATLAB. -Ken
  • Arsalan: Hi, I am very excited about the MATLAB API for editor because right now i am working on a project and i need...
  • Johannes: Since I started using matlab-emacs some days ago I never experienced Emacslink. But I experienced some...
  • Francisco J. Beron-Vera: Hi all, I have recently learned about ViEmu (http://www.vimemu.c om) which, for Vi/Vim...
  • OysterEngineer: When I first learned of the Publish feature in MatLab, I thought it might be useful to help to...
  • Ken: Hi Herve, I’m not quite sure what you mean by “stand-alone&# 8221; mode? -Ken
  • Herve: I wonder when the publish fonction will be supported in standalone mode.
  • Mike: Ravi, What you described should work as far I understand it. Please follow up with technical support. With a...
  • Mike: @Daniel, Thanks for that note.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.