Unable to use the rowfun function

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

Accepted Answer

Steven Lord
Steven Lord on 17 Jul 2017
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 Comments
Steven Lord
Steven Lord on 7 Dec 2017
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 on 7 Dec 2017
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!