2013-02-06 45 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. 如果一个简单的检查就足够了,这将有我的个人喜好。