{"id":1211,"date":"2005-10-03T15:36:04","date_gmt":"2005-10-03T20:36:04","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=1211"},"modified":"2016-05-10T09:11:09","modified_gmt":"2016-05-10T13:11:09","slug":"tip-using-html-in-the-command-window-for-useful-error-messages","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2005\/10\/03\/tip-using-html-in-the-command-window-for-useful-error-messages\/","title":{"rendered":"Tip: Using HTML in the Command Window for Useful Error Messages"},"content":{"rendered":"<p>Doug and I are taking a slight departure from our usual format this week, with a couple of MATLAB programming tips. Doug posted a link to his great new advanced GUIDE tutorial, and I&#8217;d like to highlight a feature that I&#8217;ve found really useful lately &#8211; hyperlinks in the command window.<\/p>\n<p>A good error message not only tells you what you did wrong, but tells you how to fix the problem. A great error message fixes it for you. Now that the command window supports HTML hyperlinks that run MATLAB code, it&#8217;s easy to write error messages that give the user the option of fixing problems with a click of the mouse.<\/p>\n<p>If this interests you, I suggest that you read the documentation for matlab: syntax. When you are done, take a look at this example code (you&#8217;ll need to copy\/paste it into a new file and save it as errorexample.m). I use a hyperlink in an error message to allow a user to download a required file to run an application.<\/p>\n<pre class=\"code\">function errorexample\r\n% ERROREXAMPLE  Using HTML for error messages. \r\n\r\n% Application will look for this file\r\nfilename = 'getfile.m';\r\n\r\n% which can be downloaded with this command\r\nurl = 'https:\/\/www.mathworks.com';\r\np = '\/matlabcentral\/files\/3179\/';\r\ndownloadcommand = ['urlwrite(''' ...\r\n    url p filename ...\r\n    ''',''' filename ''');'];\r\n    \r\nif ~exist(filename,'file')\r\n    error(['This application requires ' ...\r\n        filename ...\r\n        '.  Click &lt;a href=\"matlab:' ...\r\n        downloadcommand ...\r\n        '\"&gt;here&lt;\/a&gt; to download it now.'])\r\nelse\r\n    disp([mfilename ...\r\n        ' doesn''t do anything. ' ...\r\n        '&lt;a href=\"matlab: delete ' filename ...\r\n        '\"&gt;Delete ' filename ...\r\n        '&lt;\/a&gt; and run again.'])\r\nend<\/pre>\n<p>In case you noticed that the link is to one of my own File Exchange submissions and thought I was just trying to drive up my downloads, note that this backdoor way of downloading files does not impact the file counter. Not that we&#8217;re keeping score, but Doug seems to be driving you guys in droves to his submissions :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Doug and I are taking a slight departure from our usual format this week, with a couple of MATLAB programming tips. Doug posted a link to his great new advanced GUIDE tutorial, and I&#8217;d like to&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2005\/10\/03\/tip-using-html-in-the-command-window-for-useful-error-messages\/\">read more >><\/a><\/p>\n","protected":false},"author":33,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/1211"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=1211"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/1211\/revisions"}],"predecessor-version":[{"id":7015,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/1211\/revisions\/7015"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=1211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=1211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=1211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}