通常,如果我在文件中包含foo.m以下形式的注释:
% See also: <a href="http://en.wikipedia.org/etc">link name</a>
Run Code Online (Sandbox Code Playgroud)
该链接出现在帮助浏览器中,即在Matlab中,我发布
>> help foo
Run Code Online (Sandbox Code Playgroud)
我得到类似的东西
另请参见:链接名称
到现在为止还挺好.但是,有一些网址有搞笑人物,例如:
% See also: <a href="http://en.wikipedia.org/wiki/Kernel_(statistics)">http://en.wikipedia.org/wiki/Kernel_(statistics)</a>
Run Code Online (Sandbox Code Playgroud)
Matlab无法在帮助浏览器中正确呈现此内容.当我查看帮助时,它看起来像这样:
链接到名为'statistics'的本地目录.我尝试了各种引用转义和反斜杠,但无法让浏览器正常工作.
url-用字符代码转义有趣的字符。
function foo
%FOO Function with funny help links
%
% Link to <a href="http://en.wikipedia.org/wiki/Kernel_%28statistics%29">some page</a>.
Run Code Online (Sandbox Code Playgroud)
Matlab urlencode() 函数将向您显示要使用的代码。但保留冒号和斜线不变。
>> disp(urlencode('Kernel_(statistics)'))
Kernel_%28statistics%29
Run Code Online (Sandbox Code Playgroud)
这是一个将引用 URL 路径元素的函数,保留您需要完整保留的部分。
function escapedUrl = escape_url_for_helptext(url)
ixColon = find(url == ':', 1);
if isempty(ixColon)
[proto,rest] = deal('', url);
else
[proto,rest] = deal(url(1:ixColon), url(ixColon+1:end));
end
parts = regexp(rest, '/', 'split');
encodedParts = cellfun(@urlencode, parts, 'UniformOutput', false);
escapedUrl = [proto join(encodedParts, '/')];
function out = join(strs, glue)
strs(1:end-1) = strcat(strs(1:end-1), {glue});
out = cat(2, strs{:});
Run Code Online (Sandbox Code Playgroud)
要使用它,只需传入您的整个 URL 即可。
>> escape_url_for_helptext('http://en.wikipedia.org/wiki/Kernel_(statistics)')
ans =
http://en.wikipedia.org/wiki/Kernel_%28statistics%29
Run Code Online (Sandbox Code Playgroud)