1

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.

3
  • I can tell you how x can be used as an input to the anonymous function @(x1,x2,x3,..). will that be acceptable? Commented Feb 15, 2019 at 12:19
  • @SardarUsama what if x has 100 elements, it would be a disater to write down them all. Commented Feb 15, 2019 at 12:23
  • I can tell you how to do that automatically. I am not suggesting to manually write them Commented Feb 15, 2019 at 12:24

2 Answers 2

3

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{:})
Sign up to request clarification or add additional context in comments.

Comments

1

Learn from:Generate Function with Vector Input Arguments

X = sym('X', [3, 3])
F=det(X)+X(1,1)

func = matlabFunction(F,"Vars",{X})

func(rand(3))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.