14

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.

Ansari
  • 8,168
  • 2
  • 23
  • 34
zroth
  • 438
  • 4
  • 9
  • Doesn't `x(:)` work? Or if it's a column vector use `x(:)'`. – Thor Jul 12 '12 at 23:06
  • The `num2cell` route is exactly what you should be looking for. – Jonas Jul 12 '12 at 23:51
  • @Jonas Fair enough, in that that solution is simple. But I think that it's a bit silly that I have to specify a temporary variable (and, thus, have the variable's contents copied an extra time) just to do this. Why would this feature exist for cell arrays but not for the (arguably more common) numeric array (i.e., the "mat" in "matlab")? Anyhow, that's why I was asking: It just seemed like a feature that is likely supported but that I was somehow missing. – zroth Jul 13 '12 at 00:38
  • @Thor No, that does not work. Regardless of the shape of `x`, `x(:)` is always a column vector. In particular, if `x` is already a vector, then `x(:)` is just the same vector (though perhaps transposed). – zroth Jul 13 '12 at 00:42
  • Have you tried [deal](http://www.mathworks.com/help/techdoc/ref/deal.html)? – tmpearce Jul 13 '12 at 02:32
  • @tmpearce I'm aware of `deal`, but I don't think that it helps here. – zroth Jul 13 '12 at 02:56
  • `f(deal(num2str(x(:))))` lets you one-line it at least (note that it is still copied to a temporary so you're not saving memory but it is automatically cleared, etc.). – tmpearce Jul 13 '12 at 03:30
  • @tmpearce That doesn't work for me, and I can't find any documentation suggesting that `deal` can be used in that way. (Oh, and I'm assuming you meant `num2cell` and not `num2str`.) Even when using deal to assign to variables, you would need to extract the values from the cell array with `num2cell(x){:}`, which is not allowed syntax (thus the temporary variable in the original post). I'd be happy to be wrong, though. – zroth Jul 13 '12 at 04:28
  • @zroth: You can always call `subsref` – Jonas Jul 13 '12 at 11:19

1 Answers1

7

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.

Community
  • 1
  • 1
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • If `f` takes more than one parameter, this function doesn't allow you to call `f(splice(x))`. The reason is that matlab interprets the number of requested output arguments to be one. You can see this explicitly by putting `disp(nargout);` in the function definition. – zroth Jul 13 '12 at 05:10
  • Oh, you're right. I completely forgot that I could call `subsref` explicitly like that. I don't understand why your proposed `subsref` call isn't identical to calling `y{:}` for `y = num2cell([1 2])`; but, as you said, it just doesn't work. Thanks for trying. – zroth Jul 13 '12 at 15:55