Passing functions as arguments in Matlab -
i'm trying write function gets 2 arrays , name of function arguments.
e.g.
main.m: x=[0 0.2 0.4 0.6 0.8 1.0]; y=[0 0.2 0.4 0.6 0.8 1.0]; func2(x,y,'func2eq') func 2.m : function t =func2(x, y, z, 'func') //"unexpected matlab expression" error message here t= func(x,y,z); func2eq.m: function z= func2eq(x,y) z= x + sin(pi * x)* exp(y);
matlab tells gives me above error message. i've never passed function name argument before. going wrong?
you use function handles rather strings, so:
main.m
:
... func2(x, y, @func2eq); % "@" operator creates "function handle"
this simplifies func2.m
:
function t = func2(x, y, fcnhandle) t = fcnhandle(x, y); end
for more info, see documentation on function handles
Comments
Post a Comment