{"id":314,"date":"2009-05-25T17:06:04","date_gmt":"2009-05-25T17:06:04","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2009\/05\/25\/colorizing-text-output\/"},"modified":"2009-05-25T17:06:04","modified_gmt":"2009-05-25T17:06:04","slug":"colorizing-text-output","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2009\/05\/25\/colorizing-text-output\/","title":{"rendered":"Colorizing text output"},"content":{"rendered":"<p>There have been a number of requests recently asking that we support colorized output in the Command Window (<a href=\"https:\/\/blogs.mathworks.com\/community\/2007\/06\/11\/feeling-colorful\/#comment-6097\">here<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/community\/2007\/07\/09\/printing-hyperlinks-to-the-command-window\/#comment-6036\">here<\/a> and <a href=\"https:\/\/blogs.mathworks.com\/community\/2008\/12\/29\/fantastic-features-of-2008\/#comment-6028\">here<\/a>). I won&#8217;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.<\/p>\n<p>Some of the users I&#8217;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 &#8220;good&#8221; diagnostics, but also occasionally printed out a message indicating that something wasn&#8217;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&#8217;ll address below.<\/p>\n<p>We&#8217;ll start by writing a simple function that takes a string and wraps it in an HTML <tt>font<\/tt> tag. This function will also take a color, which will be used for the <tt>font<\/tt> tags color attribute. Because the result will be HTML , the color value can be anything supported by <tt>font<\/tt>&#8216;s color attribute (e.g. red, #cccccc etc.). Here&#8217;s what that function looks like:<\/p>\n<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> colorizedstring = colorizestring(color, stringtocolorize)\r\n<span style=\"color: #228B22\">%colorizestring wraps the given string in html that colors that text.<\/span>\r\n    colorizedstring = [<span style=\"color: #A020F0\">'&lt;font color=\"'<\/span>, color, <span style=\"color: #A020F0\">'\"&gt;'<\/span>, stringtocolorize, <span style=\"color: #A020F0\">'&lt;\/font&gt;'<\/span>];\r\n<span style=\"color: #0000FF\">end<\/span><\/pre>\n<\/div>\n<p>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 <tt>colorizestring<\/tt> function. Finally, close the file for writing and open it using the <tt>web<\/tt> command.<\/p>\n<p>Here&#8217;s what a sample usage might look like:<\/p>\n<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">%% Open an HTML file to write the output into.<\/span>\r\nfilename = <span style=\"color: #A020F0\">'colorizedoutput.html'<\/span>;\r\nfid = fopen(filename, <span style=\"color: #A020F0\">'wt'<\/span>);\r\n\r\n<span style=\"color: #228B22\">%% Do the long running calculation that generates lots of output.<\/span>\r\n<span style=\"color: #0000FF\">for<\/span> i=1:100\r\n    outputswitch = rand;\r\n    <span style=\"color: #0000FF\">if<\/span> rand &lt; 0.80\r\n    fprintf(fid, colorizestring(<span style=\"color: #A020F0\">'black'<\/span>, <span style=\"color: #A020F0\">'Everything is fine.&lt;br\/&gt;'<\/span>));\r\n    <span style=\"color: #0000FF\">elseif<\/span> rand &lt; 0.90\r\n        fprintf(fid, colorizestring(<span style=\"color: #A020F0\">'orange'<\/span>, <span style=\"color: #A020F0\">'There may be a problem.&lt;br\/&gt;'<\/span>));\r\n    <span style=\"color: #0000FF\">else<\/span>\r\n        fprintf(fid, colorizestring(<span style=\"color: #A020F0\">'red'<\/span>, <span style=\"color: #A020F0\">'There is a problem.&lt;br\/&gt;'<\/span>));\r\n    <span style=\"color: #0000FF\">end<\/span>\r\n\r\n    fprintf(fid, <span style=\"color: #A020F0\">'some string&lt;br\/&gt;'<\/span>);\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\n<span style=\"color: #228B22\">%% Close the HTML output file.<\/span>\r\nfclose(fid);\r\n\r\n<span style=\"color: #228B22\">%% Open the HTML output file in MATLAB's web browser.<\/span>\r\nweb(filename);<\/pre>\n<\/div>\n<p>and the resulting output:<\/p>\n<div align=\"center\">\n<a  href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/ken_orr_colorizing_text_output\/output.png\"><img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/ken_orr_colorizing_text_output\/output_small.png\"><\/a>\n<\/div>\n<p><\/br>This won&#8217;t solve everyone&#8217;s problems, but it should help to fill the gap until we can fully support colorized output in the Command Window.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There have been a number of requests recently asking that we support colorized output in the Command Window (here, here and here). I won&#8217;t be offering a way to colorize the Command Window&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2009\/05\/25\/colorizing-text-output\/\">read more >><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,9],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/314"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=314"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/314\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}