3

I have a function in matlab of the form fun(a,b,c), wherethe using may or may not give argument 'c' when he calls the function. I have to use a switch case on 'c' later in that function, and thus need to check whether user called the function with 2 or 3 arguments?

How to do that?

nrz
  • 10,435
  • 4
  • 39
  • 71
Vishal
  • 989
  • 2
  • 11
  • 19

1 Answers1

9

You can do it by using nargin:

function fun(a,b,c)

if (nargin < 3)
    c = c_default_value;
end

switch c

or using nargin and varargin (this function definition permits unlimited number of arguments):

function fun(a,b,varargin)

if (nargin < 3)
    c = c_default_value;
else
    c = varargin{1};
end

switch c
nrz
  • 10,435
  • 4
  • 39
  • 71