필터 지우기
필터 지우기

Unable to use the rowfun function

조회 수: 11 (최근 30일)
Joel Abrahams
Joel Abrahams 2017년 7월 17일
댓글: Guillaume 2017년 12월 7일
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