How to overload class functions for custom functions

9 views (last 30 days)
Built-in matlab classes allow you to specify your own function overloads by mimicking directory structure:
% Overloads 'disp' method:
@char/disp.m
How can I duplicate this behavior for my custom classes? I.e. if I have a class structure:
+MyPackage
@MyClass
myMethod.m
anotherMethod.m % I want this to stay the same in both cases
MyClass.m
How can I overload this elsewhere to have
+DifferentPackage
@MyClass
myMethod.m % alternative myMethod
Sorry if the question is poorly phrased -- I'll do my best to clarify / edit as needed

Answers (1)

Steven Lord
Steven Lord on 31 Jul 2018
Can you do this? I'm not certain. I know in previous releases having multiple class directories on the path would treat the union of the files in those directories as being the definition of the class, but that has changed. From the documentation: "In MATLAB Versions 5 through 7, class folders do not shadow other class folders having the same name, but residing in later path folders. Instead, the class uses the combination of methods from all class folders having the same name to define the class. This behavior is no longer supported."
Should you do this? I would not, as I think it would be likely that you would try to run / debug one of the versions of the method but actually be calling the other.
If you describe in more detail the application you're trying to implement using this approach we may be able to offer a different suggestion, one that avoids deliberate shadowing.
  4 Comments
Steven Lord
Steven Lord on 7 Aug 2018
I'm on vacation so your best bet is probably to contact Technical Support using the Contact Us link in the upper-right corner of this page and explain in more detail why you're trying to do this (not just what you're trying to do; there may be a different design that will achieve your goal without the complexity of your current design) and ask them for suggestions.
Sean de Wolski
Sean de Wolski on 7 Aug 2018
If they are two variations, then I'd make them both inherit from the same base class with different subclass implementations of MyMethod.

Sign in to comment.

Categories

Find more on Sample Class Implementations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!