2

I recently came to a problem that I was supposed to define a function with uncertain number of inputs, that is, the number of inputs may vary depending on the practical context. Should I take use of a 2-D array or something else? I don't know if struct2cell helps and how if it really works.

Does anyone have an idea of the best way to go about doing this?

I've probably not been very clear, so please let me know if anything needs clarifying.

Thanks

Tianhang
  • 33
  • 5

2 Answers2

6

There are several ways to go about this:

Use optional input arguments if a given parameter means the same regardless of context, but if in some scenarios, additional input is needed.

function out = myFun(first,second,third,fourth)

%# first is always needed
if nargin < 1 || isempty(first)
error('need nonempty first input')
end

%# second is optional
if nargin < 2 || isempty(second)
second = defaultValueForSecondWhichCanBeEmpty;
end

%# etc

You can call this function as out = myFun(1,[],2,3), i.e. pass an empty array for non-needed inputs.


If two inputs mean that the function is used one way, and three inputs mean that the function is used in another way (and even the inputs mean different things), use VARARGIN

function out = myFun(varargin)

%# if 2 inputs, it's scenario 1, with 3 it's scenario 2
switch nargin
case 2
   firstParameter = varargin{1};
   secondParameter = varargin{2};
   scenario = 1;
case 3
   firstParameter = varargin{1}; %# etc
otherwise
   error('myFun is only defined for two or three inputs')
end

Finally, you can also have your inputs passed as parameterName/parameterValue pairs. See for example this question on how to handle such input.

Community
  • 1
  • 1
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • Thank you Jonas. Your idea may handle this problem perfectly. But I'm afraid it won't be able to preserve its efficiency when the nargin goes way up larger than 10. Please let me know if I got it wrong. Again I really appreciate your help~ – Tianhang Mar 08 '11 at 01:37
  • @Tianhang: What do you mean with "efficiency"? Anyway, if you can set reasonable defaults for the inputs, then solution #3 is best (as seen e.g. for `plot`, where you can supply dozens of additional inputs). If you cannot easily set defaults (or estimate them from the mandatory inputs), meaning that you always have to give all 10 inputs, anyway, you should go with #1 to reduce the required amount of typing. – Jonas Mar 08 '11 at 02:12
  • Well, I've reconsidered this and I think practical context may not require over 5 inputs, so the nargin check will do. You're right. Thanks for your help. – Tianhang Mar 08 '11 at 07:09
1

You can use nargin to validate the parameters to your function, so If you need only one do some action, or if tou pass more parameters do some other action. etc.. like:

function yours(arg1,arg2,arg3,arg4,arg5,...,argn)
  if nargin < 5
    arg5 =  'Init'
  elseif (nargin > 1)
    arg2 =  'Init'
    arg3 =  'Init'
    arg4 =  'Init'
    arg5 =  'Init'
   end
end

So you can control the number of parameters you receive.

edgarmtze
  • 24,683
  • 80
  • 235
  • 386
  • Thanks for your answer. But I don't quite understand how are the nargin associated with the choice of argi. Anyway, I think I've got your idea. Thanks. – Tianhang Mar 08 '11 at 07:13