varargin - too many inputs

11 visualizzazioni (ultimi 30 giorni)
Brian
Brian il 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 Commenti
Brian
Brian il 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 il 24 Mar 2011
Ok so thats not doing it either... I get an error saying inputs must be numeric...

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 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 Commento
Brian
Brian il 24 Mar 2011
Great thanks for that. Guess I misread the {:} as (:) when I was reading up on varargin...

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by