varargin - too many inputs

조회 수: 12 (최근 30일)
Brian
Brian 2011년 3월 24일
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
Brian
Brian 2011년 3월 24일
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 2011년 3월 24일
Ok so thats not doing it either... I get an error saying inputs must be numeric...

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by