代码生成的函数句柄限制
当您在用于代码生成的 MATLAB® 代码中使用函数句柄时,请遵循以下限制:
在某些情况下,使用相同的变量引用不同函数句柄会导致编译时错误。例如,以下代码不执行编译:
function y = foo(p) x = @plus; if p x = @minus; end y = x(1, 2);
不要向 coder.ceval
传入函数句柄或从其传出函数句柄
您不能向 coder.ceval
传入函数句柄,也不能从其传出函数句柄。例如,假设 f
和 str.f
是函数句柄:
f = @sin; str.x = pi; str.f = f;
以下语句将导致编译错误:
coder.ceval('foo', @sin); coder.ceval('foo', f); coder.ceval('foo', str);
您不能创建引用 MATLAB 外部函数的函数句柄。
您不能向 feval
和其他 MATLAB 外部函数传入,或从其传出函数句柄。
您不能向入口函数传入,也不能从其传出函数句柄。以如下函数为例:
function x = plotFcn(fhandle, data) assert(isa(fhandle,'function_handle') && isa(data,'double')); plot(data, fhandle(data)); x = fhandle(data);
在此示例中,函数 plotFcn
接收一个函数句柄及其数据作为输入。plotFcn
尝试使用输入 data
调用 fhandle
引用的函数,并绘制结果。但是,此代码会生成编译错误。该错误指示在 MATLAB 函数内执行调用以指定输入的属性时,函数 isa
不会将 'function_handle'
识别为类名。
相关主题
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)