Argument passing in matlab

I have a function in matlab of the form fun (a, b, c) where use may or may not give the argument 'c' when it calls the function. I should use the case of switching to 'c' later in this function, and therefore, I need to check if the user called the function with two or three arguments?

How to do it?

+3
source share
1 answer

You can do this using nargin:

function fun(a,b,c)

if (nargin < 3)
    c = c_default_value;
end

switch c

or using narginand varargin(this function definition allows an unlimited number of arguments):

function fun(a,b,varargin)

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

switch c
+9
source

All Articles