poster
2019-12-10, 20:41
我正在尝试编写一个获取两个数组并将另一个函数的名称作为参数的函数。
例如
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告诉给我上面的错误信息。我以前从未传递过函数名称作为参数。我要去哪里错了?
回答:
您还可以使用函数句柄而不是字符串,如下所示:
main.m :
... func2(x, y, @func2eq); % The "@" operator creates a "function handle" 这简化了func2.m :
function t = func2(x, y, fcnHandle) t = fcnHandle(x, y); end 有关更多信息,请参见函数句柄 (https://mathworks.com/help/matlab/function-handles.html)的文档。
更多&回答... (https://stackoverflow.com/questions/2730029)
例如
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告诉给我上面的错误信息。我以前从未传递过函数名称作为参数。我要去哪里错了?
回答:
您还可以使用函数句柄而不是字符串,如下所示:
main.m :
... func2(x, y, @func2eq); % The "@" operator creates a "function handle" 这简化了func2.m :
function t = func2(x, y, fcnHandle) t = fcnHandle(x, y); end 有关更多信息,请参见函数句柄 (https://mathworks.com/help/matlab/function-handles.html)的文档。
更多&回答... (https://stackoverflow.com/questions/2730029)