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) %
% 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) %
% My wrapper, second attempt
inputstr = '';
for i = 1:nargin
inputstr = [inputstr 'varargin{' num2str(i) '}']; %
if i < nargin
inputstr = [inputstr ', ']; %
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?
source
share