Matlab - optional descriptor argument first for functions of type plot

Matlab includes many graphing functions in which an optional argument is an axis descriptor for graphing. There are many solutions on the Internet for adding optional arguments to user-defined functions (varargin, inputParser), however they usually require that optional arguments come only after the required arguments, while the build functions in matlab usually take the form

plot(optional, mandatory, optional) 

That is, optional arguments can appear both before and after the required arguments.

I would like to reproduce this behavior for a custom chart type so that it matches the same style as the built-in plot functions. The following use cases appear to demonstrate that checking the number of arguments alone is not enough to achieve the desired behavior:

 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 

My question is: what methods can we use to imitate this behavior?

+7
matlab plot optional-arguments
source share
2 answers

You can write a function that accepts varargin as input. Then you check the number of arguments. If it is less than 2 (or something else, depending on your function), enter an error or warning. Then check the class input parameters.

If the class your first entry is 'matlab.graphics.axis.Axes' , your function should call: plot(ax,___) . If it is double, then it must be in the format plot(X,Y,LineSpec) .

Something in this direction should work

 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 ... 

PS! You do not need to do x = varargin{1} , etc., It was just to illustrate what each of the various elements of the cell represents if if evaluates to true .

You can continue the "Arguments for the name value pair." Check if the class of the input argument is char and that it cannot represent anything other than the parameter name. If this is the name of the parameter, then you know that the next argument is the value of the parameter.

+4
source share

I usually use a template that is also used by many of the build functions that are part of 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 is an undocumented function. You always take a small risk when doing this, but it is present and unchanged in MATLAB forever, and it is used by many very stable build functions in MATLAB, so you should be fine.

What he does is check to see if the first argument is an axis descriptor. If so, then ax is the handle, and args are the rest of the input arguments. If not, ax empty, and args contains all the input arguments.

Hope this helps!


Edit: more information about axescheck on request.

First, you can see the location and source code for axescheck by typing which axescheck and edit axescheck . That way you can see exactly what he is doing.

Syntax [AX, ARGS, NARGS] = AXESCHECK(ARG1, ARG2, ...) .

First, it checks if ARG1 an axis descriptor. If so, it returns as ax , the remaining arguments ( ARG2, ... ) are returned in args , and NARGS is the value of nargin minus 1.

Secondly, it checks whether any of the input arguments is a pair of parameters and values ​​with the Parent parameter. If so, then all pairs of the pair of parameters and the Parent parameter are removed from the list. The specified axis is returned in ax , the remaining arguments are returned in args , and NARGS is the nargin value minus the number of arguments removed.

If none of the axes is indicated by any of the indicated methods, then ax empty, args are only input arguments, and NARGS is the nargin value.

axescheck works with both old-style double handles (Handle Graphics 1) and new-style descriptors (Handle Graphics 2) of the matlab.graphics.axis.Axes class.

It also checks to see if the supplied descriptor is the descriptor of the remote object, causing an error, if any.

It is quite widely used in many built-in functions of MATLAB construction - see, for example, hist.m, polar.m, surfl.m, bar3.m, comet.m, pie.m and many others.

+7
source share

All Articles