VARARGIN Variable Input Arguments

Section: Functions and Scripts


FreeMat functions can take a variable number of input arguments by setting the last argument in the argument list to varargin. This special keyword indicates that all arguments to the function (beyond the last non-varargin keyword) are assigned to a cell array named varargin available to the function. Variable argument functions are usually used when writing driver functions, i.e., functions that need to pass arguments to another function. The general syntax for a function that takes a variable number of arguments is
  function [out_1,...,out_M] = fname(in_1,..,in_M,varargin)

Inside the function body, varargin collects the arguments to fname that are not assigned to the in_k.


Here is a simple wrapper to feval that demonstrates the use of variable arguments functions.

function wrapcall(fname,varargin)

Now we show a call of the wrapcall function with a number of arguments

--> wrapcall('printf','%f...%f\n',pi,e)

A more serious driver routine could, for example, optimize a one dimensional function that takes a number of auxilliary parameters that are passed through varargin.