Unable to use the rowfun function

18 次查看(过去 30 天)
I'm attempting to use the rowfun function as such:
A = rowfun(@(x,y) etime(datevec(y), datevec(x)), B(:,3:4));
However when I do this I get the following error:
Undefined function 'rowfun' for input arguments of type 'function_handle'.
This doesn't make any sense however, as the signature of the method explicitly states that the method takes a function handle. So what am I doing wrong here?

采纳的回答

Steven Lord
Steven Lord 2017-7-17
As the documentation states, rowfun is intended to "Apply function to table or timetable rows". The second input must be a table or timetable. Is B a table or a timetable?
If you want to iterate over the rows of a numeric array, use arrayfun with a vector running from 1 to size(theNumericArray, 1) and index into rows of the numeric array inside the function handle you pass into arrayfun.
  3 个评论
Steven Lord
Steven Lord 2017-12-7
The shortest syntax for rowfun given in the documentation requires two inputs, a function handle and a table or timetable, so you must call it with at least two inputs and the second must be a table or timetable. Calling it with fewer than two inputs or calling it without the second input being a table or timetable will not work.
You can call rowfun with more than two inputs, but the number of inputs must be even (the required two plus some number of name-value pairs from the documentation.)
Guillaume
Guillaume 2017-12-7
And for the nitty-gritty of why you get undefined function when calling rowfun without any argument, it's because rowfun is not a regular function, it is a class method of table (derived from its base class tabular). Under Matlab scoping rules, class methods are only found if one of the arguments is an object of the class. Therefore rowfun will only be found if it's passed a table as one of its input (even if it is in the wrong position):
>> rowfun
Undefined function or variable 'rowfun'.
>> rowfun(table())
Not enough input arguments.
Error in tabular/rowfun (line 160)
As stated by Steven, the definition of rowfun is such that the table must be the second argument for it to work.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by