10

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?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • If it's a static method of a class, I expect `@()className.end` should work. – Jonas Mar 31 '17 at 07:00
  • @Jonas Here's what happens: `e = @(a,b,c)table.end(a,b,c); e(1,2,3)` -> `The class table has no Constant property or Static method named 'end'.` At least on my R2017a... – Dev-iL Mar 31 '17 at 07:12
  • Ah, I see now. While it's a method listed in metaClass.MethodList (`metaClass=?table`), it's not part of the class definition and thus not available via dot-reference. Hm... – Jonas Mar 31 '17 at 07:38
  • 2
    what about: `hEnd = @(A,K,N) feval('end',A,K,N);`? – user2999345 Mar 31 '17 at 08:37
  • @user2999345 Why would that work? This would likely call the `end` function of class `A` - but that's the thing - I don't want _that_ `end`, but some `B`'s `end`. Whether `B`'s function is applicable to `A` is something I (the user) will worry about later. – Dev-iL Apr 02 '17 at 10:09
  • This is not how function handles work. You simply cannot call a function overloaded for type `A` without an input argument of type `A`. You cannot define a function handle to a specific overload. You can define a function handle to `end`, and MATLAB decides which `end` to call depending on the input arguments (as in the answer by Daren Shan). This has nothing to do with anything specific about `end`. Try to do this with `size`, you'll get the same errors. – Cris Luengo Dec 07 '17 at 00:40
  • The question here is: why do you want to do this? If it is about not implementing this method for your own class, you should probably just make a soft link into your own class directory. But better is just copy-paste the code. That `end` method is absolutely trivial. – Cris Luengo Dec 07 '17 at 00:43
  • @CrisLuengo - Why do I want this? Because I am exploring the limits of MCOS (the applicability of something like "reflection"). The concept of "[Method References](https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html)" exists in other languages. "_You simply cannot call a function overloaded for type `A` without an input argument of type `A`_" - the **falsehood** of this assertion is easy to demonstrate using static methods. Since MATLAB doesn't enforce type safety, there's nothing stopping you from calling such methods with whichever arguments - and this might even work! – Dev-iL Dec 07 '17 at 10:06
  • That is true, static methods do not require an object of the class as input. They don't require inputs at all. So this would work with static methods. I've been using MATLAB since the 90's, static methods are kinda new to me... :) But I an sure that this cannot be used with "regular" methods, the functions that you stick in a `@xxx` directory so that they are specific to that type (and by extension, also those defined in `classdef` files). And I'm glad it's not possible, you could break a lot of my code if it were! – Cris Luengo Dec 07 '17 at 14:28
  • Do you have a link for MCOS? Google doesn't know what it is (or rather, there are at least 10 other things that it thinks are more important). I'm curious to learn about it! – Cris Luengo Dec 08 '17 at 02:14
  • By the way, a static method is not an overloaded method. – Cris Luengo Dec 08 '17 at 02:14
  • @CrisLuengo MCOS stands for "MATLAB class object system", I believe I encountered this terminology on [undocumentedmatlab.com](https://undocumentedmatlab.com/blog/tag/mcos). – Dev-iL Dec 08 '17 at 08:42
  • Perhaps some of the confusion stems from a problem with the question, since I should've written "override" (as in redefine in a subclass) and not "overload" (as in having several methods with the same name only differing by inputs/outputs; which is not supported in MATLAB, although there are ways around it using `varargin`, `nargin`, etc.). – Dev-iL Dec 08 '17 at 09:13

1 Answers1

1

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

Hadi
  • 1,203
  • 1
  • 10
  • 20
  • This missed the point of the question. I didn't ask how MATLAB decides which overloaded method to call, nor did I say I had a `table` object for MATLAB to resolve the correct `end` function. What I have is some other class (or structure), which is _modeled after_ `table` (but does not inherit from it), such that it _could_ make use of `table`'s `end` - if only I could link to that function somehow... Arguably, if we were talking about a private instance method, my question would be invalid, but here we are just talking about a handle to a function defined in an `.m` file with a special name... – Dev-iL Apr 02 '17 at 09:04
  • I don't think this answer misses the point of the question. You cannot get the handle to specific overloads. You get the handle to the `end` function, and MATLAB decides which one to call depending on the input arguments. – Cris Luengo Dec 07 '17 at 00:36