Overload only several functions

3 views (last 30 days)
Hi,
I am totally new to Matlab OOP. For my program to work, I need to redefine the built-in min, max and abs functions. However, if I declare these three functions as methods, all the other functions like sine, cosine, etc. must be declared so that they act on the defined object. So how can I only redefine (overload) these three functions and let the others unchanged.
Thanks, Zoli
  2 Comments
Image Analyst
Image Analyst on 18 Oct 2014
I believe redefining is usually called overriding, not overloading which is somewhat different because with overloading you can have different functions and it figures out which to use based on what inputs you pass and outputs you accept.
Zoltán Csáti
Zoltán Csáti on 20 Oct 2014
Thank you for explaining the difference.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 18 Oct 2014
It sounds like you're trying to implement a numeric class. So I'll refer you to matlab own's documentation on subclassing built-in classes
If you derive from a numeric class (e.g. double), you'll automatically get all the methods that apply to double, and you can override the one you want.
Otherwise, there's no other way than you writing all of them, even if it's just a dispatch to the built-in ones.
  6 Comments
Guillaume
Guillaume on 20 Oct 2014
As I said:
With matlab class system, I'm afraid there's no way around it but to override the methods in your class and just dispatch to the base class.
There's not that many so it shouldn't be too much of a hassle
Zoltán Csáti
Zoltán Csáti on 20 Oct 2014
Thank you, I will do it.

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 18 Oct 2014
Zoltán - have you created a new class that you wish to implement the max, min, and abs functions for? Or are you trying to overload these three functions for all data types?
If the former, then why not try something like the following
% class definition
classdef MyClass < handle
% private data members
properties (Access=public)
attribute1;
attribute2;
end
methods (Access=public)
% class constructor
function [obj] = MyClass(attr1,attr2)
obj.attribute1 = attr1;
obj.attribute2 = attr2;
end
% returns the instance of MyClass in the array with the greatest
% attribute2 property
function [maxObj] = max(myClassArray)
maxObj = myClassArray(1);
for k=2:length(myClassArray)
if maxObj.attribute2 < myClassArray(k).attribute2
maxObj = myClassArray(k);
end
end
end
% returns the instance of MyClass in the array with the smallest
% attribute2 property
function [maxObj] = min(myClassArray)
maxObj = myClassArray(1);
for k=2:length(myClassArray)
if maxObj.attribute2 > myClassArray(k).attribute2
maxObj = myClassArray(k);
end
end
end
function display(obj)
fprintf('Attribute 1 = %f Attribute 2 = %f\n',...
obj.attribute1, obj.attribute2);
end
end
end
The above definition is for a very simple class with two attributes. We define the max and min methods to determine that instance (of this class) in the input array that has the maximum attribute2 property and the minimum attribute2 respectively. For example,
myObjA = MyClass(1,2);
myObjB = MyClass(3,4);
myArray = [myObjA myObjB];
maxObj = max(myArray);
minObj = min(myArray);
In the above, we can see that maxObj corresponds to myObjB, and minObj corresponds to myObjA.
  3 Comments
Geoff Hayes
Geoff Hayes on 18 Oct 2014
Try using the builtin function so we can call the MATLAB builtin function from our overloaded method.
Add the following method to the above class
% calculate the sine of attribute 1
function [result] = sin(obj)
result = builtin('sin',obj.attribute1);
end
Now, try the following
clear all;
myObjA = MyClass(pi/4,12);
sin(myObjA)
and we get the expected answer of sin(pi/4) as
ans =
0.707106781186547
As for your question concerning handle, see the abstract class for deriving handle classes for more details.
Zoltán Csáti
Zoltán Csáti on 18 Oct 2014
Thank you too, especially for the link pointing at subclass and builtin.

Sign in to comment.

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!