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?
@()className.end
should work. – Overweighte = @(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... – WestermanmetaClass=?table
), it's not part of the class definition and thus not available via dot-reference. Hm... – OverweighthEnd = @(A,K,N) feval('end',A,K,N);
? – Peplumend
function of classA
- but that's the thing - I don't want thatend
, but someB
'send
. WhetherB
's function is applicable toA
is something I (the user) will worry about later. – WestermanA
without an input argument of typeA
. You cannot define a function handle to a specific overload. You can define a function handle toend
, and MATLAB decides whichend
to call depending on the input arguments (as in the answer by Daren Shan). This has nothing to do with anything specific aboutend
. Try to do this withsize
, you'll get the same errors. – Barajasend
method is absolutely trivial. – BarajasA
without an input argument of typeA
" - 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! – Westerman@xxx
directory so that they are specific to that type (and by extension, also those defined inclassdef
files). And I'm glad it's not possible, you could break a lot of my code if it were! – Barajasvarargin
,nargin
, etc.). – Westerman