我通常使用这样的模式,MATLAB 中的许多绘图函数也使用这种模式:
function varargout = myplot(obj, varargin)
% Check the number of output arguments.
nargoutchk(0,1);
% Parse possible axes input.
[ax, args, ~] = axescheck(varargin{:}); %#ok<ASGLU>
% Get handle to either the requested or a new axis.
if isempty(ax)
hax = gca;
else
hax = ax;
end
% At this point, hax refers either to a specified axis, or
% to a fresh one if none was specified. args refers to the
% remainder of any arguments passed in varargin.
% Parse the rest of args
% Make the plot in hax
% Output a handle to the axes if requested.
if nargout == 1
varargout{1} = hax;
end
end
axescheck 是一个未记录的函数。这样做总是冒着很小的风险,但它在 MATLAB 中一直存在并且从未改变,并且它被 MATLAB 中许多非常稳定的绘图函数使用,所以你应该没问题。
它的作用是检查第一个参数是否是轴的句柄。如果是,那么ax 是那个句柄,args 是其余的输入参数。如果不是,则ax 为空,args 包含所有输入参数。
希望有帮助!
编辑:按要求提供有关axescheck 的更多信息。
首先,您可以通过键入which axescheck 和edit axescheck 来查看axescheck 的位置和源代码。通过这种方式,您可以准确地看到它的作用。
语法是[AX, ARGS, NARGS] = AXESCHECK(ARG1, ARG2, ...)。
首先,它检查ARG1 是否是轴的句柄。如果是,则返回为AX,其余参数(ARG2, ...)在ARGS 中返回,NARGS 是nargin 的值减1。
其次,它检查任何输入参数是否是带有参数Parent 的参数值对。如果是这样,则从列表中删除所有带有参数Parent 的参数-值对。指定轴在AX中返回,其余参数在ARGS中返回,NARGS是nargin的值减去被移除的参数个数。
如果以上两种方式都没有指定轴,则AX为空,ARGS只是输入参数,NARGS是nargin的值。
axescheck 可与 matlab.graphics.axis.Axes 类的旧式(句柄图形 1)双句柄和新式(句柄图形 2)句柄一起使用。
它还检查提供的句柄是否是已删除对象的句柄,如果是则抛出错误。
它在许多内置的 MATLAB 绘图函数中得到广泛使用 - 例如,请参阅 hist.m, polar.m, surfl.m, bar3.m, comet.m, pie.m 和许多其他函数。