strange error with varfun and anonymous function

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Bjorn Gustavsson
Bjorn Gustavsson il 21 Dic 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

Più risposte (0)

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by