How to wrap a function using varargin and varargout?

Tobias Kienzler picture Tobias Kienzler · Feb 4, 2011 · Viewed 12k times · Source

mini-example:

function varargout = wrapper(varargin)
varargout = someFunction(varargin);

That's how I'd do it first. But for example if someFunction = ndgrid this yields a not defined for cell arrays error, so the next try was using someFunction(varargin{:}) instead. That's a successful call, but calling [a,b] = wrapper([1,2], [3,4]) does not yield the same result as a direct call to ndgrid, so what am I doing wrong?

Answer

SCFrench picture SCFrench · Feb 6, 2011

Actually, Mikhail's answer is not quite right. In the case that someFunction is a function that returns a value even if none is requested, which is how a function indicates that the value should be assigned to ans, Mikhail's wrapper will fail. For example, if someFunction were replaced with sin and you compared running wrapper versus running sin directly, you'd see:

>> wrapper(0)
>> sin(0)

ans =

   0

The right way to do this is

function varargout = wrapper( varargin )
[varargout{1:nargout}] = someFunction( varargin{:} ); 

The reason this works is due to a little known edge case in MATLAB indexing rules that has existed precisely for this case since at least R2006a (probably longer). It is something of a wart in MATLAB indexing but was deemed necessary to handle this sort of thing.

The rule is:

When performing subscripted assignment, if

  • subscripted-assigning to an uninitialized variable, AND
  • the uninitialized variable is curly-brace indexed, AND
  • the index in the curly braces is empty, AND
  • the left-hand side appears inside square braces, AND
  • the right-hand side resolves to a value / returns an output

Then the uninitialized variable is assigned a scalar cell containing the value returned by the right-hand side.

For example:

>> clear uninit % just to make sure uninit is uninitialized
>> [uninit{[]}] = sin(0)

uninit = 

    [0]