1

MATLAB 和工具箱中的许多绘图函数(并非全部)都允许以下两种语法:

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

第一个绘制到当前坐标区 ( gca) 或创建并绘制到新坐标轴(如果不存在)。第二个用句柄绘制到轴中axes_handle

研究了几个 MATLAB 和工具箱绘图函数的内部结构后,看起来 MathWorks 并没有真正的标准化方式来执行此操作。一些绘图例程使用内部但开放的函数axescheck来解析输入参数;有些人对第一个输入参数进行简单检查;有些使用更复杂的输入解析子功能,可以处理更多种类的输入语法。

请注意,这axescheck似乎使用了一种未记录的语法ishghandle- 文档说ishghandle只接受一个输入,如果它是任何 Handle Graphics 对象,则返回 true;但axescheck将其称为ishghandle(h, 'axes'),仅当它专门是一个轴对象时才返回 true 。

有人知道实现这种语法的最佳实践或标准吗?如果不是,您发现哪种方式最稳健?

4

3 回答 3

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  
于 2017-07-25T14:45:30.617 回答
0

不确定我是否理解这个问题。我所做的是将数据的绘图与绘图的生成/设置分开。因此,如果我想以标准化的方式绘制直方图,我有一个调用函数setup_histogram(some, params),它将返回适当的句柄。然后我有一个函数update_histogram(with, some, data, and, params)将数据写入适当的句柄。

如果您必须以相同的方式绘制大量数据,这非常有效。

于 2013-02-06T15:27:13.493 回答
0

副业的两个建议:

  1. 如果你不需要,不要去无证。
  2. 如果一个简单的检查就足够了,这将是我个人的偏好。
于 2013-02-06T16:50:19.440 回答