Passing functions as arguments in Matlab
Solution 1:
You could also use function handles rather than strings, like so:
main.m
:
...
func2(x, y, @func2eq); % The "@" operator creates a "function handle"
This simplifies func2.m
:
function t = func2(x, y, fcnHandle)
t = fcnHandle(x, y);
end
For more info, see the documentation on function handles
Solution 2:
You could try in func2.m
:
function t = func2(x, y, funcName) % no quotes around funcName
func = str2func(funcName)
t = func(x, y)
end