Implement Wrapper Functions

You can use varargin and varargout to write wrapper functions that accept up to 64 inputs and pass them directly to another function.

Passing Variable Numbers of Arguments from One Function to Another

The following example passes a variable number of inputs to different optimization functions, based on a specified input method:

function answer = fcn(method,a,b,c)
answer = optimize(method,a,b,c);

function answer = optimize(method,varargin)
  if strcmp(method,'simple')
    answer = simple_optimization(varargin{:});
    answer = complex_optimization(varargin{:});

Key Points About the Example

  • You can use {:} to read all elements of varargin and pass them to another function.

  • You can mix variable and fixed numbers of arguments.

For more information, see Variable Length Argument Lists for Code Generation.

Was this topic helpful?