How to wrap a function using varargin and varargout?


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?


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 =


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 = 


function varargout = wrapper( varargin )

if ~nargout
    someFunction( varargin{:} ); 
    [varargout{1:nargout}] = someFunction( varargin{:} ); 

If the number of output arguments is the same as the number of input arguments, you can use

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

This works fine with ndgrid.

Need Your Help

.NET Identity Email/Username change

Does anyone know how to enable a user to change username/email with ASP.NET identity with email confirmation? There's plenty of examples on how to change the password but I can't find anything on t...

How to create a table in Android with multiple columns?

java android xml android-tablelayout

I want to create a table in android with multiple column. Most of the examples I saw is with 2 columns. (I am new to Java and Android.) I need 3-4 columns and I should be able to add the rows dynam...