-
Notifications
You must be signed in to change notification settings - Fork 13
/
hyperlink.m
56 lines (50 loc) · 1.96 KB
/
hyperlink.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function str = hyperlink(url, label, msg)
%HYPERLINK create a string that is displayable as hyperlink in Matlab console
%
% Usage examples:
% fprintf('Search on %s\n', hyperlink('http://google.com','Google'));
% fprintf(hyperlink('http://google.com','Google','Search on Google\n'));
%
% HYPERLINK converts the specified URL and text label into a string that is
% displayed as a hyperlink in the Matlab console (Command Window).
% In a deployed (compiled) program, the URL and text label (if different
% from the URL) are displayed in a non-hyperlinked plain-text manner.
% If the optional 3rd input argument (msg) is specified, then all instances of
% the specified label within msg will be handled as above (hyperlinked etc.)
%
% IN:
% url - (mandatory) URL of webpage or Matlab command (e.g., 'matlab:which(...')
% label - (optional) text label of the hyperlink. Default: url
% msg - (optional) string in which all label instances should be hyperlinked
%
% OUT:
% str - string output
% Copyright: Yair Altman 2020-
%{
% 15/01/20 - Initial version
%}
narginchk(1,3);
if nargin > 2 % msg was specified
% replace all instances of label within msg with corresponding hyperlink
str = strrep(msg, label, hyperlink(url,label));
return
end
if nargin < 2, label = url; end
isWebpage = strncmpi(url,'http',4);
% Only hyperlink in interactive Matlab sessions, not in a deployed program
if ~isdeployed % interactive Matlab session
if isWebpage % open in a web browser
str = ['<a href="matlab:web(''-browser'',''' url ''');">' label '</a>'];
else % Matlab command e.g. 'matlab:which(...'
str = ['<a href="' url '">' label '</a>'];
end
else % deployed (compiled)
if isWebpage && ~strcmp(label,url) % display label next to url
str = [label ' (' url ')'];
elseif isWebpage % text==url - display only the url
str = url;
else % Matlab command (not a webpage) - just display the label
str = label;
end
end
end