6

When you have a function that takes a variable amount of arguments (like ndgrid), how can you pass an arbitrary list of arguments to that function?

For example I want to make it so that sometimes I pass two vectors to ndgrid and get out two matrices, i.e.,

[X1,X2] = ndgrid(x1,x2);

But other times I might have more X's, so I'll want

[X1,X2,X3,X4] = ndgrid(x1,x2,x3,x4)
  1. Is there any kind of structure I can use to store a list of an unknown number of arguments and then just give that list to a function? And,
  2. Is there a way to retrieve all of the outputs from a function, when you don't know how many there will be?
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
rkp
  • 438
  • 5
  • 9
  • a quick and dirty way would be to abstract your input and output up a level ("dimension") and take an array of inputs and output an array - that is [X] = ndgrid(x) where X and x are cell arrays or something, with each element an actual input element – im so confused Oct 31 '12 at 20:32
  • I am not well-versed enough, however, to state definitively if MATLAB has a `varargs` type functionality (I'm sure it does as it can compile into C) – im so confused Oct 31 '12 at 20:33
  • oh it's remarkably simple: http://www.mathworks.com/help/matlab/ref/varargin.html – im so confused Oct 31 '12 at 20:35
  • 2
    FWIW, this is a different question than the current marked duplicate (http://stackoverflow.com/q/5215244/931379). That question asks how to create a new function with multiple inputs/outputs; the answer is varargin/varargout. This question is really how to programmatically use such a function with an unknown number of arguments. – Pursuit Jun 10 '16 at 22:47

3 Answers3

7

To pass in a variable number of inputs to an existing function, use cell arrays with expansion, like this:

x = 1:10;
y = randn(size(x));
plotArguments = {'color' 'red' 'linestyle' '-'};
plot(x, y, plotArguments{:});

or

plotArguments = {1:10 randn(1,10)  'color' 'red' 'linestyle' '-'};
plot(plotArguments{:});

You can use the same trick to receive multiple numbers of outputs. The only hard part is remembering the correct notations.

numArgumentsToAccept = 2;
[results{1:numArgumentsToAccept }] = max(randn(100,1));
Pursuit
  • 12,285
  • 1
  • 25
  • 41
  • Thanks! This is exactly what I was looking for. I figured it had something to do with cell arrays, but I didn't know the notation. – rkp Nov 01 '12 at 18:07
4

Using varargin,nargin, varargout and nargout you can easily define variable argument/output functions. See the attached MATLAB documentation link for the varargin page. The others are linked at the bottom:

http://www.mathworks.com/help/matlab/ref/varargin.html

EDIT: BTW, not to toot my own horn, but it seems to be implemented just as I had suggested in the "quick-and-dirty" comment hehehe

im so confused
  • 2,091
  • 1
  • 16
  • 25
  • This doesn't address the question of passing a variable number of segregated arguments into a pre-existing function. sub2ind() for example of the example of ngrid as given in the OP. – GaetaWoo Jun 16 '19 at 00:23
0

a function that returns all arguments as outputs:

function varargout = ndgrid(varargin)    
    varargout = varargin;
return
Serg
  • 13,470
  • 8
  • 36
  • 47