Search code examples
matlabfunctionoctavecell-arraymultiple-arguments

Octave input arguments to function as cell array


I want to input arguments to an octave function as a cell array:

function x = myfunc(a_string, an_int)
  printf("a string: %s\n", a_string);
  printf("an int: %d\n", an_int);
end

myfunc("a", 1);
b = {"a", 1};
myfunc(b); % should do the same thing as myfunc("a", 1)

Is there any way to do this easily?


Solution

  • You need to use {:} indexing to expand the contents of the cell array out into multiple inputs to your function. The {:} indexing creates a comma separated list which behaves just as multiple inputs.

    myfunc(b{:})