Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a function handle to an overloaded `end` function

MATLAB allows overloading various operators for custom classes. One of the unlisted overloadable operators is end, as can be learned from \matlab\lang\end.m:

%   END(A,K,N) is called for indexing expressions involving the object A
%   when END is part of the K-th index out of N indices.  For example,
%   the expression A(end-1,:) calls A's END method with END(A,1,2).

An example of such a method is table.end (paste in the MATLAB command line and press "Open Selection" to go to its definition; it is defined in ...\matlab\datatypes\@tabular\end.m).

Unlike a normal method, one cannot simply write hEnd = @end, because this gives the error:

>> hEnd = @end;
 hEnd = @end;
         ↑
Error: Illegal use of reserved keyword "end".

On the other hand, writing e = str2func('end'); works, but it links to the default end function (even when temporarily switching to the folder where the desired end.m is found).

Failed attempts include str2func('table>end');, str2func('table\end');, str2func('table.end'); and @(a,b,c)table.end(a,b,c);.

My question: How do I create a handle to the end function of a specific class?

like image 995
Dev-iL Avatar asked Mar 31 '17 06:03

Dev-iL


People also ask

How do you create a function handle?

Function handles can represent either named or anonymous functions. To create a function handle, use the @ operator. For example, create a handle to an anonymous function that evaluates the expression x2 – y2: f = @(x,y) (x.

How does a function handle work?

Function handles are variables that you can pass to other functions. For example, calculate the integral of x2 on the range [0,1]. q = integral(f,0,1); Function handles store their absolute path, so when you have a valid handle, you can invoke the function from any location.

How do you use Varargin?

Specify varargin by using lowercase characters. After any explicitly declared inputs, include varargin as the last input argument . When the function executes, varargin is a 1-by-N cell array, where N is the number of inputs that the function receives after the explicitly declared inputs.


1 Answers

Overloading — If the function you specify overloads a function in a class that is not a fundamental MATLAB class, the function is not associated with the function handle at the time it is constructed. Instead, MATLAB considers the input arguments and determines which implementation to call at the time of evaluation.


Function handles store their absolute path, so when you have a valid handle, you can invoke the function from any location. You do not have to specify the path to the function when creating the handle, only the function name.


so if your 'end' function is in matlab path , matlab consider it as a candidate for evaluation depending on the inputs,in your case if input object is of 'table' class type the feval(str2func('end'),i,j) evaluate the end function which is defined in the folder @table/end.m

like image 193
Hadi Avatar answered Sep 20 '22 12:09

Hadi