Matlab - 可选的句柄参数首先用于类似函数的绘图

bhi*_*lam 7 matlab plot optional-arguments

Matlab包含许多绘图函数,它们将可选参数作为绘制轴的句柄.在线有许多解决方案可以为用户定义的函数添加可选参数(varargin,inputParser),但是它们通常要求可选参数仅在强制参数之后,而在matlab中绘制函数通常是

plot(optional, mandatory, optional)
Run Code Online (Sandbox Code Playgroud)

也就是说,可选参数可以在强制参数之前和之后出现.

我想为自定义绘图类型复制此行为,以便它遵循与内置绘图函数相同的样式.以下用例表示仅检查参数的数量不足以实现所需的行为:

x = [1:10];
y = x.^2;
ax(1) = subplot(1, 2, 1);
ax(2) = subplot(1, 2, 2);

myplot(x, y);                 %Mandatory
myplot(x, y, 'r+');           %Mandatory, optional
myplot(ax(1), x, y);          %Optional, mandatory
myplot(ax(2), x, y, 'r+');    %Optional, mandatory, optional
Run Code Online (Sandbox Code Playgroud)

我的问题是,我们可以使用哪些技术来模拟这种行为?

Sam*_*rts 7

我通常使用这样的模式,它也被许多作为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
Run Code Online (Sandbox Code Playgroud)

axescheck是一个无证的功能.这样做你总是冒小风险,但它在MATLAB中一直存在并且没有变化,并且在MATLAB中被许多非常稳定的绘图函数使用,所以你应该没问题.

它的作用是检查第一个参数是否是轴的句柄.如果是,则ax是该句柄,并且args是输入参数的其余部分.如果没有,ax则为空并args包含所有输入参数.

希望有所帮助!


编辑:有关axescheck所请求的更多信息.

首先,您可以axescheck通过键入which axescheck和查看位置和源代码edit axescheck.通过这种方式,您可以准确地看到它的作用.

语法是[AX, ARGS, NARGS] = AXESCHECK(ARG1, ARG2, ...).

首先,它检查是否ARG1是轴的句柄.如果是这样,它返回为AX,返回剩余的arguments(ARG2, ...)ARGS,并且NARGS值为nargin-1.

其次,它检查任何输入参数是否是带参数的参数值对Parent.如果是,Parent则从列表中删除所有带参数的参数值对.返回指定的轴,返回AX其余参数ARGS,并且NARGSnargin减去已删除参数数量的值.

如果在上述任何一种方式中都没有指定轴,那么它AX是空的,ARGS只是输入参数,并且NARGS是值nargin.

axescheck适用于旧式(Handle Graphics 1)双手柄和类的新式(Handle Graphics 2)手柄matlab.graphics.axis.Axes.

它还检查提供的句柄是否是已删除对象的句柄,如果是,则抛出错误.

它在许多内置的MATLAB绘图功能中得到了广泛的应用 - 例如,参见hist.m, polar.m, surfl.m, bar3.m, comet.m, pie.m许多其他功能.


Ste*_*fin 4

您可以编写一个作为输入的函数varargin。然后,您检查参数的数量。如果它小于2(或其他值,取决于您的函数),则抛出错误或警告。然后,检查class输入参数的。

如果class您的第一个输入是'matlab.graphics.axis.Axes',那么您的函数应该调用:plot(ax,___)。如果是双精度数,那么它一定是格式plot(X,Y,LineSpec)

沿着这些思路的东西应该有效

function [] = myplot(varargin)

if nargin < 2
   error('Minimum two input must be given'); % You probably want something other than an error. This was just an example.
elseif nargin == 2
    % Code for plotting
    plot(x, y)
elseif nargin == 3
    if strcmp(class(varargin{1}),'matlab.graphics.axis.Axes')
       ax1 = varargin{1};
       x = varargin{2};
       y = varargin{3};
       plot(ax1, x, y)
    elseif isa(varargin{2}, 'double') && isa(varargin{3}, 'double') && isa(varargin{3}, 'char')
       x = varargin{1};
       y = varargin{2};
       LineSpec = varargin{3};
     else ...
Run Code Online (Sandbox Code Playgroud)

附言!您不需要执行其他操作,这只是为了说明如果计算结果为 ,x = varargin{1}每个不同的单元格元素代表什么。iftrue

您可以继续“名称-值对参数”。检查输入参数的类是否为char,并且它不能表示除参数名称之外的其他内容。如果它是参数名称,那么您就知道下一个参数是参数值。