strange error with varfun and anonymous function

6 views (last 30 days)
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

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 21 Dec 2022
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

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!