MATLAB: How to use str2func with a function handle in the string

MATLABstr2func

Hi,
I want to insert a function handle in the input string of str2func but I get an error.
string1 = '@(x)sin(x)';
A = str2func(string1);
string2 = '@(x,y)(A(x) + sin(y))'
B = str2func(string2);
B(pi/2,pi/2)
It says: Undefined function or variable 'A'.
How can I fix it?
Thanks

Best Answer

"Workspace variables are not available to the str2func function."
See this answer. You could do something like this:
string1 = '@(x)sin(x)';
A = str2func(string1);
string2 = ['@(y)(' num2str(feval(A,pi/2)) ' + sin(y))'];
B = str2func(string2);
B(pi/2)