Converting a vector to multiple variables required as input for 'matlabFunction'.

I am converting symbolic function to a Matlab function using 'matlabFunction'.
For e.g.
syms x y
g=matlabFunction(x+y-1)
Output
g = @(x,y)x+y-1.0
The returned matlab function 'g' accepts individual variables like g(1,1). However, I want to give inputs as a vector, like,
input = [1 1]
g(input)
I get error 'Not enough input arguments.' I want to give input as a single vector because number of input in my actual function vary during run-time. Any idea how to convert a vector to multiple variable at run-time efficiently or any other solution.

 采纳的回答

input = {1 1}
g(input{:})

5 个评论

Thanks a lot Andrei Bobrov. This works perfectly and is in form I was looking for. Thanks again :)
This thread relates to my problem, which is how to use matlabFunction with fsolve, which annoyingly will only accept anonymous functions with a single, possible vector argument, while matlabFunction insists on returning multiple scalar arguments. I'd hoped that I could use Andre's approach but fsolve is pretty unforgiving:
syms x y
g = matlabFunction([x+y,x-y]);
input = {x y}
so far so good, but then
fsolve(@(input{:}) g(input{:}),[0,0])
returns an error: Unbalanced or unexpected parenthesis or bracket
while
fsolve(@(input) g(input{:}),[0,0])
returns an error: Cell contents reference from a non-cell array object
Can anybody suggest how to successfully interface matlabFunction with fsolve? (Obviously it is easy to avoid matlabFunction with this trivial problem, but for my real problem, matlabFunction is clearly the best way to go.)
The parser treats textual occurrences of {:} different than computed versions, so you need a temporary variable. The easiest way to handle that is to introduce a true function such as
function r = call_on(func, array)
t = num2cell(array);
r = func(t{:});
end
fsolve(@(x) call_on(g, x), [0,0])
Thanks Walter
I could do everything without anonymous functions, but would prefer not to because of another infuriating matlab property which is that I can't define a function within an m-file, have to do it within a function. Should I just give up on passing an anonymous function to fsolve? I'm willing to do it the hard way if there is a hard way!
I do not understand what you mean about can't define a function within an m-file ?
The code I show above involves passing anonymous functions to fsolve: it just uses a helper function which would be the same for all cases. The "g" that I reference in the expression call_on(g, x) is the anonymous function constructed via matlabFunction. The version that does not use this (invariant) helper function would need to use some nasty MATLAB behavior to fudge the temporary variable.

请先登录,再进行评论。

更多回答(2 个)

The matlab documentation fails to mention that it's possible to set this up with matlabFunciton() itself. I've discovered that how you input the 'Vars' argument adjusts how the function is built. Using a vector input of variables for 'Vars' will give you what you want. See the following example:
syms x y
z = x+y;
Without vector input:
matlabFunction(z, 'Vars', {x, y})
function_handle with value:
@(x,y)x+y
With vector input:
matlabFunction(z, 'Vars', {[x, y]})
function_handle with value:
@(in1)in1(:,1)+in1(:,2)

3 个评论

Wow! I've been using the ol'"g(input{:})" trick for years until I stumbled onto your answer. Specifying "Vars" as a vector in the matlabFunction function works like a charm!!!
Oh my gosh this works so well! You just saved me many hours. God bless you sir!
This should be the accepted answer. So much more elegant than defining temporary variables that get created and then removed at run time.

请先登录,再进行评论。

g = @(x,y)x+y-1.0
%g is a function of two inputs x and y, if you want one vector input
g=@(x) x(1)+x(2)-1.0
%then
g([2 3])
=4

1 个评论

Many thanks Azzi Abdelmalek for the fast reply. However, I am unable implement it. The 'matlabFunction' returns function of multiple variables at run-time and I cannot change input variables easily.

请先登录,再进行评论。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by