varargin - too many inputs

5 views (last 30 days)
Brian
Brian on 24 Mar 2011
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?
  2 Comments
Brian
Brian on 24 Mar 2011
Actually I could just have n included in the varargin part... dumb...
However I still have n defined throughout my code so I'd still like to know if what I'm trying to do above is possible without including n in varargin...
Brian
Brian on 24 Mar 2011
Ok so thats not doing it either... I get an error saying inputs must be numeric...

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Mar 2011
Y = fun(n, varargin{:});
That is, you need to {:} expand the cell array, not pass the cell array as a column vector.
  1 Comment
Brian
Brian on 24 Mar 2011
Great thanks for that. Guess I misread the {:} as (:) when I was reading up on varargin...

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!