how to do function argument evaluation in matlab

In MATLAB, function arguments are evaluated when the function is called. MATLAB provides two methods for passing arguments to functions: positional arguments and named arguments.

Positional arguments are passed in the order in which they are listed in the function definition. For example, consider the following function definition:

main.m
function y = myfunc(a,b,c)
   y = a + b + c;
end
49 chars
4 lines

You can call this function with positional arguments as follows:

main.m
>> y = myfunc(1, 2, 3)
y =
     6
34 chars
4 lines

Named arguments can be passed to a function in any order by specifying the argument name followed by its value using the syntax argumentName = value. Consider the same function as before but with named arguments:

main.m
function y = myfunc(a,b,c)
   y = a + b + c;
end
49 chars
4 lines

You can call this function with named arguments as follows:

main.m
>> y = myfunc(c=3, a=1, b=2)
y =
     6
40 chars
4 lines

In both cases, the arguments are evaluated before being passed to the function. If an argument is a variable or an expression, MATLAB evaluates it before passing it to the function.

gistlibby LogSnag