Overload only several functions

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

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.
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

Thank you, this is exactly I was looking for!
One more question. I did it, but when a function from the superclass (that is double now) is applied to my property, it will return a double. How can I convert this value to my class? My code is:
classdef complexredefine < double
%UNTITLED2 Summary of this class goes here
% Detailed explanation goes here
properties
val
end
methods
function obj = complexredefine(a)
obj = obj@double(a);
obj.val = a;
end
function y = abs(x)
if real(x.val) < 0
y = complexredefine(complex(-real(x.val),-imag(x.val)));
end
end
end
end
First I create the object: c = complexredefine(-1);. Then if I call abs(c), it has the same class. But when I invoke e.g. cos(c) it returns a double of course. I would like to convert this numeric double value to complexredefine, i.e. the val property contains the double value of cos(c).
Thank you
Unfortunately, the documentation is quite explicit about this. Built-in methods that operate on data values return objects of the superclass. 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. You still get free inheritance of the indexing, reshaping, concatenating, etc. methods, as long as your subclass does not have any properties.
In your example above, you've defined a property but it serves no purpose as it stores the exact same value as the one stored in the double base class. You could rewrite your class as:
classdef complexredefine < double
methods
function obj = complexredefine(a)
if nargin < 1 %you should also add that for user-friendliness
a = 0;
end
obj = obj@double(a);
end
function y = abs(obj)
if real(obj) < 0
y = complexredefine(complex(-real(x),-imag(x)));
else
y = abs@double(obj);
end
end
end
end
and it would work just the same with the addition that you can no index into it.
Thanks, but it still does not solve my problem. After I call any function but not abs, the result will be double and the redefined abs will not be called since it will operate on a double, not on a complexredefine object.
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
Thank you, I will do it.

Sign in to comment.

More Answers (1)

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

Except the first line (I do not see why the < handle is there), I understand the code. Up to this point I managed to get to alone. And how can I achieve that the value of the normal sine function is returned when I call sin(myObjA)? In this way it can't be done since sine is not defined for this instance of the class. I can only perform e.g. sin(myObjA.attribute1). I would like it to work as follows: the user defines an object, call it obj, which has one property: val. Those 3 functions has the altered behaviour on that object but when an other function is applied to it, it retains the default behaviour, as if we typed sin(obj.val). So I would like to achieve it without typing the cumbersome sin(obj.val), just sine(obj).
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.
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!