strange error with varfun and anonymous function

6 Ansichten (letzte 30 Tage)
clauper
clauper am 21 Dez. 2022
Kommentiert: Bjorn Gustavsson am 22 Dez. 2022
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

Akzeptierte Antwort

Bjorn Gustavsson
Bjorn Gustavsson am 21 Dez. 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
  2 Kommentare
clauper
clauper am 21 Dez. 2022
Thanks! I just remove the @ and it works!
Bjorn Gustavsson
Bjorn Gustavsson am 22 Dez. 2022
Great that it worked.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by