为MATLAB plot函数实现多种语法 [英] Implementing multiple syntaxes for a MATLAB plot function
问题描述
MATLAB和工具箱中的许多绘图功能(并非全部)都支持以下两种语法:
Many of the plotting functions in MATLAB and toolboxes (thought not all) allow both the following syntaxes:
plotfcn(data1, data2, ...)
plotfcn(axes_handle, data1, data2, ...)
第一个绘制到当前轴(gca
)中,或者创建并绘制到新轴(如果不存在).第二个以手柄axes_handle
绘制到轴中.
The first plots into the current axes (gca
) or creates and plots into a new axes if none exists. The second plots into the axes with handle axes_handle
.
研究了几个MATLAB和工具箱绘图函数的内部结构,看来MathWorks并没有真正标准化的方法.一些绘图例程使用内部但开放的函数axescheck
来解析输入参数.有些人对第一个输入参数进行简单检查;有些使用更复杂的输入解析子功能,可以处理更多种输入语法.
Having looked into the internals of several MATLAB and toolbox plotting functions, it looks like there isn't really a standardised way that MathWorks do this. Some plotting routines use the internal, but open, function axescheck
to parse the input arguments; some do a simple check on the first input argument; and some use a more complex input-parsing subfunction that can handle a larger variety of input syntaxes.
请注意,axescheck
似乎使用了未记录的语法ishghandle
-文档说ishghandle
仅接受一个输入,如果它是任何Handle Graphics对象,则返回true;否则,返回true.但是axescheck
将其称为ishghandle(h, 'axes')
,仅当它是轴对象时才返回true.
Note that axescheck
appears to use an undocumented syntax of ishghandle
- the doc says that ishghandle
takes only one input, returning true if it is any Handle Graphics object; but axescheck
calls it as ishghandle(h, 'axes')
, which returns true only if it's specifically an axes object.
有人知道实现此语法的最佳实践或标准吗?如果没有,您发现哪种方法最可靠?
Is anyone aware of a best practice or standard for implementing this syntax? If not, which way have you found to be most robust?
推荐答案
如果仍然有人感兴趣,在我发布问题四年后,这就是我主要解决的模式.
In case anyone is still interested, four years after I posted the question, this is the pattern that I have mostly settled on.
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
这篇关于为MATLAB plot函数实现多种语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!