为什么在错误的上下文中使用了此函数句柄?
Why is this function handle being used in the incorrect context?
我正在尝试了解如何将函数传递给 varfun
,我想这适用于 arrayfun
、cellfun
等
阅读帮助文件,第一个参数应该是:
Function, specified as a function handle. You can define the function in a file or as an anonymous function. If func corresponds to more than one function file (that is, if func represents a set of overloaded functions), MATLAB determines which function to call based on the class of the input arguments.
所以我尝试使用以下数据:
sampleId = [1 1 1 3 3 3]';
entity = [1 2 3 1 4 5]';
dataTable = table(sampleId, entity)
是的:
varfun(@mean, dataTable)
ans =
mean_sampleId mean_entity
_____________ ___________
2 2.6667
现在,当我匿名定义自己的函数时,我的问题就出现了,例如:
mymean = @(x){sum(x)/length(x)};
然后报错:
varfun(@mymean, dataTable)
Error: "mymean" was previously used as a variable, conflicting with its use here as the name of a function or command.
See "How MATLAB Recognizes Command Syntax" in the MATLAB documentation for details.
然而,如果我不使用 at 符号,我会得到:
varfun(mymean, dataTable)
ans =
Fun_sampleId Fun_entity
____________ __________
[2] [2.6667]
我觉得我一定是在错误的上下文中使用了函数句柄 @
。谁能启发我? (注意,如评论中所述,ans
的显示很奇怪,因为 mymean
returns 是一个元胞数组。这是一个无意的错误)。
在第一个代码片段中,mean
是该函数的 (named) function, and @mean
is a function handle。您可以等效地使用
f = @mean;
varfun(f, dataTable)
第二种情况,当你定义
mymean = @(x){sum(x)/length(x)};
@(x){sum(x)/length(x)}
部分是一个 anonymous function,变量 mymean
又是一个 函数句柄 到那个(匿名)函数。所以你需要使用varfun(mymean, dataTable)
,而不是varfun(@mymean, dataTable)
。
因此,正在使用 @
符号 in two different ways,尽管在这两种情况下它都会生成一个函数句柄:
- 案例 1:从命名函数创建函数句柄。命名函数是在其自己的文件中定义的函数。
- 情况 2:作为匿名函数定义的一部分。匿名函数是直接定义的,而不是在单独的文件中。该定义构造了一个匿名函数并自动 returns 该函数的句柄。
我正在尝试了解如何将函数传递给 varfun
,我想这适用于 arrayfun
、cellfun
等
阅读帮助文件,第一个参数应该是:
Function, specified as a function handle. You can define the function in a file or as an anonymous function. If func corresponds to more than one function file (that is, if func represents a set of overloaded functions), MATLAB determines which function to call based on the class of the input arguments.
所以我尝试使用以下数据:
sampleId = [1 1 1 3 3 3]';
entity = [1 2 3 1 4 5]';
dataTable = table(sampleId, entity)
是的:
varfun(@mean, dataTable)
ans =
mean_sampleId mean_entity
_____________ ___________
2 2.6667
现在,当我匿名定义自己的函数时,我的问题就出现了,例如:
mymean = @(x){sum(x)/length(x)};
然后报错:
varfun(@mymean, dataTable)
Error: "mymean" was previously used as a variable, conflicting with its use here as the name of a function or command.
See "How MATLAB Recognizes Command Syntax" in the MATLAB documentation for details.
然而,如果我不使用 at 符号,我会得到:
varfun(mymean, dataTable)
ans =
Fun_sampleId Fun_entity
____________ __________
[2] [2.6667]
我觉得我一定是在错误的上下文中使用了函数句柄 @
。谁能启发我? (注意,如评论中所述,ans
的显示很奇怪,因为 mymean
returns 是一个元胞数组。这是一个无意的错误)。
在第一个代码片段中,mean
是该函数的 (named) function, and @mean
is a function handle。您可以等效地使用
f = @mean;
varfun(f, dataTable)
第二种情况,当你定义
mymean = @(x){sum(x)/length(x)};
@(x){sum(x)/length(x)}
部分是一个 anonymous function,变量 mymean
又是一个 函数句柄 到那个(匿名)函数。所以你需要使用varfun(mymean, dataTable)
,而不是varfun(@mymean, dataTable)
。
因此,正在使用 @
符号 in two different ways,尽管在这两种情况下它都会生成一个函数句柄:
- 案例 1:从命名函数创建函数句柄。命名函数是在其自己的文件中定义的函数。
- 情况 2:作为匿名函数定义的一部分。匿名函数是直接定义的,而不是在单独的文件中。该定义构造了一个匿名函数并自动 returns 该函数的句柄。