MATLAB:匿名函数 'might be used incompatibly or redefined'

MATLAB: anonymous functions 'might be used incompatibly or redefined'

我在执行以下操作时在第三行收到此错误:

hist_paramd = @(x) hist(x, -1:0.1:1)
data_cell = num2cell(data)
histograms = cellfun(@hist_paramd,data_cell)

这是什么意思,我该如何解决?

编辑:从原来的问题更改为原来的 rowfun 在我的 MATLAB 版本中甚至不可用。

该错误几乎可以肯定与您在 cellfun 中不需要 @hist_paramd 而只需要 hist_paramd 这一事实有关。这是因为 hist_paramd 已经是一个函数句柄(是一个匿名函数)。在创建命名函数句柄(即在 m 文件或内置函数中定义的函数)时,您只需要 @

histograms = cellfun(hist_paramd,data_cell) %anonymous function
%histograms = cellfun(@sin,data_cell) %named function

如果你想象一下cellfun中hist_paramd的定义就很容易理解了:

histograms = cellfun(@(x) hist(x, -1:0.1:1),data_cell);

显然你不需要另一个 @