MATLAB: Varargin – too many inputs

functionvarargin

Hey folks I have a function which takes other functions (call them fun1, fun2, etc) as inputs.
Some of these functions have variable length inputs with some having just one input. This is where I'm coming into trouble with varargin as my function is recognizing it as an input when it's not defined.
So my code is something like…
function myfun(fun, t, n, varargin)
% where n and varargin are the inputs to fun % Then later in the code I have
Y = fun(n, varargin(:))
% but it is here that there is a problem. This works fine when I have to specify what varargin is but if my input function fun only has one input (which would be n) then I recieve an error saying there is too many inputs.
I tried to implement some sort of loop which went like…
if nargin(fun)==1 in = n; else in = (n, varargin(:)); end;
then do Y = fun(in) but I sort of knew that wouldn't work… The error I recieve is that the input must be numeric.
So anyone know how I can solve this?

Best Answer

Y = fun(n, varargin{:});
That is, you need to {:} expand the cell array, not pass the cell array as a column vector.