2013-02-06 43 views
1

許多的MATLAB和工具箱的繪圖功能(認爲不是所有的)允許下面的兩個語法:實現多個語法的MATLAB繪圖功能

plotfcn(data1, data2, ...) 
plotfcn(axes_handle, data1, data2, ...) 

第一個情節到當前軸(gca)或創建並繪製成新的軸,如果沒有的話。第二個繪製到手柄爲axes_handle的軸上。

查看了幾個MATLAB和工具箱繪圖函數的內部結構後,看起來好像MathWorks沒有真正的標準化方法。一些繪圖例程使用內部函數axescheck解析輸入參數;有些人會對第一個輸入參數做一個簡單的檢查;有些使用更復雜的輸入解析子函數,可以處理更多種類的輸入語法。

注意axescheck似乎使用的ishghandle無證語法 - 美國商務部說,ishghandle只需要一個輸入,返回true,如果它是任何處理圖形對象;但axescheck將其稱爲ishghandle(h, 'axes'),僅當它特別是一個軸對象時才返回true。

是否有人知道實現此語法的最佳實踐或標準?如果不是,你發現哪種方式最健壯?

+0

FYI:http://undocumentedmatlab.com/blog/ishghandle-undocumented-input-parameter/ –

+0

@Yair - 謝謝你的namecheck! –

回答

1

如果有人仍然感興趣,我發佈問題四年後,這是我主要解決的模式。

function varargout = myplotfcn(varargin) 
% MYPLOTFCN Example plotting function. 
% 
% MYPLOTFCN(...) creates an example plot. 
% 
% MYPLOTFCN(AXES_HANDLE, ...) plots into the axes object with handle 
% AXES_HANDLE instead of the current axes object (gca). 
% 
% H = MYPLOTFCN(...) returns the handle of the axes of the plot. 

% Check the number of output arguments. 
nargoutchk(0,1); 

% Parse possible axes input. 
[cax, args, ~] = axescheck(varargin{:}); 

% Get handle to either the requested or a new axis. 
if isempty(cax) 
    hax = gca; 
else 
    hax = cax; 
end 

% At this point, |hax| refers either to a supplied axes handle, 
% or to |gca| if none was supplied; and |args| is a cell array of the 
% remaining inputs, just like a normal |varargin| input. 

% Set hold to on, retaining the previous hold state to reinstate later. 
prevHoldState = ishold(hax); 
hold(hax, 'on')   


% Do the actual plotting here, plotting into |hax| using |args|. 


% Set the hold state of the axis to its previous state. 
switch prevHoldState 
    case 0 
     hold(hax,'off') 
    case 1 
     hold(hax,'on') 
end 

% Output a handle to the axes if requested. 
if nargout == 1 
    varargout{1} = hax; 
end 
0

不知道我明白這個問題。 我所做的是將繪圖的數據與繪圖的生成/設置分開。所以如果我想以標準化的方式繪製直方圖,我有一個名爲setup_histogram(some, params)的函數,它將返回相應的句柄。然後我有一個功能update_histogram(with, some, data, and, params)它將數據寫入適當的句柄。

如果您必須以相同的方式繪製大量數據,這種方式非常有效。從邊線

0

兩個建議:

  1. ,如果你不需要不要去無證。
  2. 如果一個簡單的檢查就足夠了,這將有我的個人喜好。