1function count = hprintf (varargin)
2%HPRINTF fprintf with hypertext links not highlighted in the command window.
3% hprintf does this by replacing the string "href=" with "HREF=".  If the file
4% descriptor is not present, the output defaults to the command window, just
5% like fprintf.  Note that all browsers accept either href= or HREF=, so your
6% hypertext links will not be affected except within the MATLAB Command Window.
7% General usage is identical to fprintf:
8%
9%      hprintf (format, arg1, arg2, ...)
10%      hprintf (fid, format, arg1, arg2, ...)
11%      count = hprintf ( ... same as above ... )
12%
13% Example:
14%      fprintf ('<a href="http://www.mathworks.com">MathWorks</a>\n') ;
15%      fprintf ('<a href="http://hitchhikers.movies.go.com">%d</a>\n', 42) ;
16%      hprintf ('<a href="http://www.mathworks.com">MathWorks</a>\n') ;
17%      hprintf ('<a href="http://hitchhikers.movies.go.com">%d</a>\n', 42) ;
18%
19% For a discussion, see Kristin's blog and the comments there at
20% http://blogs.mathworks.com/desktop/2007/07/09
21% (<a href="http://blogs.mathworks.com/desktop/2007/07/09">Kristin's blog</a>).
22%
23% NOTE: the examples above are modified by "help hprintf" so that you cannot
24% see the HREF= text.  To see the examples properly (without hypertext
25% highlighting) use:
26%
27%      edit hprintf
28%
29% To try the examples above, use hprintf with no inputs (note that this usage
30% of hprintf also flags an error, to exactly mimic the fprintf behavior):
31%
32%      hprintf
33%
34% Here is a slightly more complex example that has the advantage of being
35% printed properly by "help hprintf":
36%
37%      % a string template with hypertext contents:
38%      str = '<a AREF="http://www.mathworks.com">MathWorks</a>\n' ;
39%      % made into an active hypertext, which will be underlined when
40%      % displayed in the command window:
41%      hstr = strrep (str, 'AREF', 'href') ;
42%      fprintf (hstr) ;
43%      %          displays: '<a href="http://www.mathworks.com">MathWorks</a>'
44%      hprintf (hstr) ;
45%      %          displays: '<a HREF="http://www.mathworks.com">MathWorks</a>'
46%
47% See also fprintf, strrep, sprintf.
48
49% Copyright 2007, T. Davis, with thanks to 'us' (us at neurol dot unizh dot ch)
50% for suggestions.
51
52% Aug 25, 2007
53
54if (nargin < 1)
55
56    % try hprintf
57    help hprintf
58    fprintf ('\nhypertext highlighting with fprintf:\n\n') ;
59    fprintf ('<a href="http://www.mathworks.com">MathWorks</a>\n') ;
60    fprintf ('<a href="http://hitchhikers.movies.go.com">%d</a>\n', 42) ;
61    fprintf ('\nhypertext highlighting turned off with hprintf:\n\n') ;
62    hprintf ('<a href="http://www.mathworks.com">MathWorks</a>\n') ;
63    hprintf ('<a href="http://hitchhikers.movies.go.com">%d</a>\n\n', 42) ;
64
65    % flag an error, to mimic fprintf behavior
66    error ('Not enough input arguments') ;
67
68elseif (nargout > 1)
69
70    % mimic fprintf
71    error ('Too many output arguments') ;
72
73else
74
75    if (ischar (varargin {1}))
76        % mimic fprintf ('hello world %d\n', 42), with no file ID
77        cnt = fprintf (strrep (sprintf (varargin {:}), 'href=', 'HREF=')) ;
78    else
79        % mimic fprintf (fid, 'hello world %d\n', 42), with file ID given
80        cnt = fprintf (varargin {1}, ...
81            strrep (sprintf (varargin {2:end}), 'href=', 'HREF=')) ;
82    end
83    if (nargout > 0)
84        % return the fprintf output
85        count = cnt ;
86    end
87end
88
89