Passing functions as arguments in Matlab

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

Leave a Comment