inconsitent function overloading MATLAB R2017a

Hello,
I am currently encountering problems with function overloading within MATLAB R2017a.
I try to overload common functions such as abs and max among others. It seems to be simple but in fact for me it is not.
To do this, I create specific directories OVERLOAD\@single, OVERLOAD\@int32, OVERLOAD\@uint32 and OVERLOAD\@double. abs and max functions are redefined into @double and @single.
Overloaded abs function into @single is defined as: function [ output ] = abs(x) ...
overloaded max function is defined as: function [ output ] = max(varargin) ...
After adding path to OVERLOAD parent directory at ht beginning of path, I execute "which -all" command trying to understand which function is effectively called when executing abs and max. This is what I get:
>> which abs -all;
D:\Data\OVERLOAD\@single\abs.m % single method
D:\Data\OVERLOAD\@double\abs.m % double method
built-in (C:\Program Files\Matlab\R2017a\toolbox\matlab\elfun\@double\abs) % double method
...
>> which max -all;
built-in (C:\Program Files\Matlab\R2017a\toolbox\matlab\datafun\max)
D:\Data\OVERLOAD\@single\max.m % single method
D:\Data\OVERLOAD\@double\max.m % double method
...
It seems that overloading for max function appears in second position after built-in instead of first position.
Could anyone explain me why overloading is successful for abs and fails for max ?
Thanks for your attention.

6 Comments

I try to overload common functions such as abs and max among others.
Why? Overloading those functions for single and double will affect all other functions that call abs or max, which may impact the operation of (or completely break) functions that you haven't overloaded.
Jan
Jan on 13 Nov 2018
Edited: Jan on 13 Nov 2018
[Moved from section for answers] Didier Billard wrote
OK I know that overloadiing such functions is impacting all the oprations using those overloaded functions.
My question, no offense, is not to know if I should do that or not, my question is to understand why this overloading is ssucessful for abs function and apprently fails for max.
Best regards.
@Didier Billard: And which function is called for max(8,9)? Set a breakpoint in the @double\max.m file to see, if the bultin or overloaded function is called. If it is the overloaded one, it would not be a "fail".
Mea culpa,
I was wrong, overloading max is working well.
It seems that I did not call the right function as max was overloaded for single but I execute max for uint32.
I can certify that function overloaded for single max is successfully called when I call max(single(8),single(9)) for example.
In fact, the first line displayed when executing "which max -all;" i.e.
built-in (C:\Program Files\Matlab\R2017a\toolbox\matlab\datafun\max)
does not mean that this is this code which is executing when calling max for single variables.
Sorry for the trouble, this was my fault; my request can be closed.
Thank you nevertheless for your response.
Best regards.
Try
which max(single(1),single(1))
Or
which abs(single(1))
The default max takes multiple types but abs is overloaded for all numeric data types.
@Didier Billard: We do not close this question, because it might help others with similar questions. Please post your "solution" as an answer and accept it to mark this thread as "solved". Even such "wrong questions" are very useful and valuable for this forum.

Sign in to comment.

 Accepted Answer

AT last, the first line displayed when executing "which max -all;" i.e.
built-in (C:\Program Files\Matlab\R2017a\toolbox\matlab\datafun\max)
does not mean that this is this code which is executing when calling max for single variables.
Thus overloading max function is successful as for abs function.

More Answers (0)

Categories

Products

Release

R2017a

Community Treasure Hunt

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

Start Hunting!