Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab --- splice vector into arguments for function call

Tags:

matlab

I want to splice a vector into a function call, but I can't find a way to do this. Is it possible?

To expand on what I mean, say we have the vector x of length n and a function f that takes n arguments. I want to be able to call f(x(1), x(2), ..., x(n)) by calling something like f(x) or f(splice(x)). If x were a cell array instead of a vector, calling f(x{:}) would get the desired result; it only seems reasonable that there would be some equivalent for when x is a vector.

I'm hoping for some operator or function that I'm missing. I could just call y = num2cell(x) followed by f(y{:}), but this is not really what I'm looking for.

like image 842
zroth Avatar asked Jul 12 '12 23:07

zroth


1 Answers

As already mentioned in the comments

tmp = num2cell(x)
f(tmp{:})

is the way to go.

A function splice so that f(splice(x)) would do what you want, doesn't do the trick. Even if you can split the input into multiple outputs, f only takes the first argument (similarly to if you were to call the function at command line without requesting outputs).

Not even subsref will work in this case, since e.g. subsref(num2cell([1 2]),struct('type','{}','subs',{{':'}})) is going to do the same as the splice-function, i.e. only return one output unless multiple outputs have been requested explicitly.

like image 53
Jonas Avatar answered Oct 25 '22 00:10

Jonas