2010-12-14 76 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