MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在Matlab中将函数作为参数传递 (https://www.labfans.com/bbs/showthread.php?t=23367)

poster 2019-12-10 20:41

在Matlab中将函数作为参数传递
 
我正在尝试编写一个获取两个数组并将另一个函数的名称作为参数的函数。

例如

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告诉给我上面的错误信息。我以前从未传递过函数名称作为参数。我要去哪里错了?



[B]回答:[/B]

您还可以使用函数句柄而不是字符串,如下所示:

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 有关更多信息,请参见[URL="https://mathworks.com/help/matlab/function-handles.html"]函数句柄[/URL]的文档。



[url=https://stackoverflow.com/questions/2730029]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 00:21

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.