フィルターのクリア

strange error with varfun and anonymous function

5 ビュー (過去 30 日間)
clauper
clauper 2022 年 12 月 21 日
コメント済み: Bjorn Gustavsson 2022 年 12 月 22 日
The following example:
tbl = table(randn(5,1));
func = @(x) cumsum(x);
varfun(@func, tbl)
gives me the error:
Error using tabular/varfun>dfltErrHandler (line 436)
Applying the function 'func' to the variable 'Var1' generated the following error:
Undefined function 'func' for input arguments of type 'double'.
Is this a bug in matlab, or am I doing something wrong?
P.S. varfun(@cumsum, tbl) works without error

採用された回答

Bjorn Gustavsson
Bjorn Gustavsson 2022 年 12 月 21 日
These 2 modifications work:
tbl = table(randn(5,1));
func = @(x) cumsum(x);
varfun(func, tbl)
% ans =
% 5x1 table
% Fun_Var1
% ________
% 0.53767
% 2.3716
% 0.11271
% 0.97488
% 1.2936
varfun(@(x) func(x), tbl)
I've (that is a major warning-flag!) have come to see this as func being a function handle, not the function itself function. varfun expects a function-handle, and I've come to see your construct:
varfun(@func, tbl)
as
varfun(@ @cumsum, tbl)
Which would be handle-function-handle or some such wrong-order type.
HTH
  2 件のコメント
clauper
clauper 2022 年 12 月 21 日
Thanks! I just remove the @ and it works!
Bjorn Gustavsson
Bjorn Gustavsson 2022 年 12 月 22 日
Great that it worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by