2

Possible Duplicate:
How do I retrieve the names of function parameters in matlab?

I'm looking for a way to get all the arguments passed to, or expected my function. The args() command seems to be ideal but is only available for procedures. Is there anyway of doing this.

My reason I want to do this is so I can do my checking in fewer lines. ie check all inputs are numeric by writing one check then executing for all args. So if there is a good alternative I'm open to ideas.

Thanks

Community
  • 1
  • 1
wookie1
  • 511
  • 6
  • 18

2 Answers2

3

What you are looking for is varargin which enables you to work with variable number of input arguments.

petrichor
  • 6,459
  • 4
  • 36
  • 48
  • I don't actually want a variable number of arguments though. And for most of the code want to work with named variables to make it clearer. I just want a way to complete my asserts without essentially repeating code. – wookie1 Oct 05 '12 at 12:06
3

You can use varargin, as petrichor mentioned. varargin is a cell, so you can easily perform validation of all your parameters in one line using cellfun:

function c = test(varargin)
cellfun(@(arg)validateattributes(arg, {'numeric'}, {'integer'}), varargin);

The above code runs validateattributes for all function parameters. On the other hand, if you want named variables, you can still group them into cells and run specific tests as above:

function c = test(i1, i2, d1, d2)

% validate integer arguments
cellfun(@(arg)validateattributes(arg, {'numeric'}, {'integer'}), {i1, i2});

% validate double arguments
cellfun(@(arg)validateattributes(arg, {'double'}, {'positive'}), {d1, d2});
angainor
  • 11,760
  • 2
  • 36
  • 56