2010-12-14 33 views
5

通常,如果我有,文件foo.m,形式的注释中:如何让Matlab帮助正确显示怪异网址的网页链接?

% See also: <a href="http://en.wikipedia.org/etc">link name</a> 

的链接出现在帮助browswer,即在Matlab,我发出

>> help foo 

和我得到像

另请参阅:link name

迄今为止这么好。不过,也有有一些奇怪的字符一些网络地址,例如:

% See also: <a href="http://en.wikipedia.org/wiki/Kernel_(statistics)">http://en.wikipedia.org/wiki/Kernel_(statistics)</a> 

Matlab的并不在帮助浏览器正确呈现这一点。当我查看帮助,它看起来像这样:

参见: statistics)“> http://en.wikipedia.org/wiki/Kernel_(statistics

其中链路在名为“统计”的本地目录。我已经尝试了各种报价逃逸和反斜线的,但不能得到帮助浏览器才能正常工作。

回答

4

URL的转义字符代码奇怪的字符。

function foo 
%FOO Function with funny help links 
% 
% Link to <a href="http://en.wikipedia.org/wiki/Kernel_%28statistics%29">some page</a>. 

Matlab的urlencode()函数将显示您要使用的代码。但保持冒号和斜线。

>> disp(urlencode('Kernel_(statistics)')) 
Kernel_%28statistics%29 

这是一个函数,它会引用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{:}); 

要使用它,只需传入整个URL即可。

>> escape_url_for_helptext('http://en.wikipedia.org/wiki/Kernel_(statistics)') 
ans = 
http://en.wikipedia.org/wiki/Kernel_%28statistics%29 
+0

为了完整起见,我必须在链接文本中转义:'%另请参阅:http://en.wikipedia.org/wiki/Kernel_%28statistics%29'。如果我在''对中有Kernel_(统计信息),Matlab不会正确显示它。谢谢你的收获,我因为没有看到它而sla头。 – shabbychef 2010-12-14 18:16:23