MATLAB: How to replace variable name in function handle

functionhandles

I have a function handle as given below
f = @(x1,x2) x1*x2
and I want to change variable name to
f = @(x) x(1)*x(2)
Please help me
Thanks
PS: The function is just for illustration purpose

Best Answer

Hi,
for me this worked:
f = @(x1,x2) x1*x2
f = string(char(f));
f = replace(f,"@(x1,x2)","@(x)");
f = replace(f,["x1","x2"],["x(1)","x(2)"]);
f = str2func(f)
result is:
f =
function_handle with value:
@(x1,x2)x1*x2
f =
function_handle with value:
@(x)x(1)*x(2)
Best regards
Stephan