1

Given a multiple-input MATLAB function

out=f(in1, in2) 

I would like to write a second function g which generates the inputs for f, e.g.

[in1, in2]=g(in)

so that I can call something like:

out=f(g(in))

I have tried writing g as a single output function that stores in1 and in2 in a cell array so that I can feed the output of g to f using the colon operator:

in_c=g(in);
out=f(in_c{:})

but I was looking for a one-line solution, which seems not possible to achieve this way as I read in this Stack Overflow question

Is there any other way to do this?

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Gianni
  • 458
  • 5
  • 17
  • This is not possible in Matlab. The best you could do is to have `f` take a cell array as input, and `g` return a cell array as output. – Chris Taylor Jul 16 '12 at 12:53
  • possible duplicate of [Matlab --- splice vector into arguments for function call](http://stackoverflow.com/questions/11461963/matlab-splice-vector-into-arguments-for-function-call) – Jonas Jul 16 '12 at 13:01

1 Answers1

0

As discussed recently, this is not possible in Matlab.

However, if you do not want to re-write your function g(x,y) to return a cell array, you can still do everything in two lines:

[in4f{1}, in4f{2}] = g(in);
out = f(in4f{:});

As an aside: Unless you're really hurting for memory, it doesn't make a lot of sense to try and force one-line statements everywhere by avoiding temporary variables. Sure, you can make your code look like CrazyPerl, but in the long run, you'll be glad for the added readability.

Community
  • 1
  • 1
Jonas
  • 74,690
  • 10
  • 137
  • 177