为MATLAB绘图函数实现多种语法

时间:2013-02-06 14:59:36

标签: matlab

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。

是否有人了解实施此语法的最佳做法或标准?如果没有,你发现哪种方式最强大?

3 个答案:

答案 0 :(得分: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  

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

来自副业的两条建议:

  1. 如果您不需要,请不要记录。
  2. 如果进行简单检查就足够了,这可能是我个人的偏好。
相关问题