Sanjay Manohar
Sanjay Manohar

Reputation: 7026

How many arguments does a MATLAB function take?

Is there a way to find out how many arguments a function will take?

like this?

f = @(x,y,z) x+y+z;
numargs( f )

ans = 
      3

I know many functions take varargin, so maybe that would return inf or something.

Upvotes: 1

Views: 150

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

You can use nargin to get the number of input arguments.

As for when a function takes varargin as input argument, the output of nargin will be negative.

Example from the docs:

function mynewplot(x,y,varargin)
   figure
   plot(x,y,varargin{:})
   title('My New Plot')

Calling nargin like so:

fx = 'mynewplot';
nargin(fx)

yields a result of -3, hence the 3rd input argument is varargin.

Upvotes: 8

Related Questions