Unpacking varargin in separate variables

I am writing a wrapper for a function that accepts varargin as its inputs. I want to keep the signature of the function in the shell, but nesting vararginleads to the union of the whole variable.

function inner(varargin) %#ok<VANUS>
% An existing function
disp(nargin)
end

function outer(varargin)
% My wrapper
inner(varargin);
end

outer('foo', 1:3, {})   % Uh-oh, this is 1

I need a way to unzip varargininto an external function, so I have a list of individual variables. There is a very nasty way to do this by constructing a string of variable names for passing innerand calling eval.

function outer2(varargin) %#ok<VANUS>
% My wrapper, second attempt
inputstr = '';
for i = 1:nargin
   inputstr = [inputstr 'varargin{' num2str(i) '}']; %#ok<AGROW>
   if i < nargin
      inputstr = [inputstr ', ']; %#ok<AGROW>
   end
end    
eval(['inner(' inputstr ')']);
end

outer2('foo', 1:3, {})   % 3, as it should be

Can someone think of a less disgusting way of doing something, please?

+5
source share
1 answer

Inside an internal external call should be

inner(varargin{:})

, varargin , , . .

+10

All Articles