Matlab: How to specify input in matlabFunction?

2019-08-04 19:43发布

问题:

matlabFunction() is a function that can convert symbolic to anonymous function. But how to specify what input arguments to be appeared on the anonymous function?

For example,

x = sym('x', [3, 1])
func = matlabFunction(x)

It returns a handle with:

func =

  function_handle with value:

    @(x1,x2,x3)[x1;x2;x3]

But how to make this to be returned:?

@(x) [x(1); x(2); x(3)]

that the whole x is the input arguments, not every element of it. This could be extremely useful when x has very long colums.

回答1:

Instead of making that anonymous function, you can input the elements of x as a comma separated list to func by first converting x to a cell array.

xcell = num2cell(x);
func(xcell{:})