Argument passing in Matlab

argument-passing, arguments, matlab, matlab-deployment, parameters

Solution

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

Problem

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?

Original source

Related problems