# Tip: Using HTML in the Command Window for Useful Error Messages

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’d like to highlight a feature that I’ve found really useful lately – hyperlinks in the command window.

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’s easy to write error messages that give the user the option of fixing problems with a click of the mouse.

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’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.

function errorexample
% ERROREXAMPLE  Using HTML for error messages.

% Application will look for this file
filename = 'getfile.m';

% which can be downloaded with this command
url = 'https://www.mathworks.com';
p = '/matlabcentral/files/3179/';
url p filename ...
''',''' filename ''');'];

if ~exist(filename,'file')
error(['This application requires ' ...
filename ...
'.  Click <a href="matlab:' ...
else
disp([mfilename ...
' doesn''t do anything. ' ...
'<a href="matlab: delete ' filename ...
'">Delete ' filename ...
'</a> and run again.'])
end

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’re keeping score, but Doug seems to be driving you guys in droves to his submissions :)

|