0
votes

So the fairly common solution to passing variable list of arguments into a subfunction is to pass in:

function out = foo(arg1, arg2, varargin)
   % do something
   out = foo2(arg1+arg2, varargin{:});
end

where varargin{:} is the comma-seperated unpacking of a cell-array.

But I run into the issue when I want to pass the variable list of arguments into a SUB-subfunction, for example

function out = foo2(arg1, varargin)
    tempOut = foo3(varargin{:});
    out = arg1 + tempOut; % Assuming I guarantee nargin >= 2
end

The problem that I run into is:

varargin passed into foo2: 1 x nargin cell

varargin passed into foo3: 1 x 1 cell, content: 1 x nargin cell

How do I unpack correctly so that I can pass into foo3 properly? Thanks!

1
oops, sorry that was a typo, i do call it with the braces in my code - ezhang94
What is the problem with foo3? Can you give a reproducible example? - Daniel
If you're calling foo3(varargin{:}); rather than foo3(varargin);, I don't see how varargin would be a "1 x 1 cell, content: 1 x nargin cell" inside foo3. Does your actual code do other things? - horchler

1 Answers

0
votes

My recommendation is to change the format of foo2 and foo3.

If foo2 is:

function out = foo2(arg1, myvarargin)
    %// Now myvarargin is a cell array.
    tempOut = foo3(myvarargin);
    out = arg1 + tempOut; % Assuming I guarantee nargin >= 2
end

and its called as

out = foo2(arg1+arg2, varargin);

It will work (dependent on the declaration of foo3). This is because you don't use "variable arguments" in foo2 but you have an input that is a cell array, that can vary of size. But it will always be a single intput.