strange error with varfun and anonymous function

6 vues (au cours des 30 derniers jours)
clauper
clauper le 21 Déc 2022
Commenté : Bjorn Gustavsson le 22 Déc 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

Réponse acceptée

Bjorn Gustavsson
Bjorn Gustavsson le 21 Déc 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 commentaires
clauper
clauper le 21 Déc 2022
Thanks! I just remove the @ and it works!
Bjorn Gustavsson
Bjorn Gustavsson le 22 Déc 2022
Great that it worked.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by