I am trying superclass / subclass relationships for the first time. I have defined a subclass, M, with two super classes, S & R. My subclass has data in it and I want to call a superclass method from each superclass to add data to the subclass.
When I call the superclass method, it is called and executes correctly on the subclass object. However, when it returns from the call, the subclass object remains unchanged. I'm sure it's something obvious I'm missing but I've searched the forums and can't quite seem to find what I'm looking for.
I created the following files as a generic example that illustrates what I'm seeing:
M.m:
classdef M < S & R
properties
data
end
methods
function obj = M(a, b, c)
obj@S(a,b);
obj@R(a,c);
obj.data = repmat([b c], a, 1);
disp('Original M Object:');
disp(obj);
disp(obj.data);
obj.addSdata;
disp('M Object after calling S Method:');
disp(obj);
disp(obj.data);
disp('M object after calling R Method:');
obj.addRdata;
disp(obj);
disp(obj.data);
end
end
end
S.m:
classdef S
properties (Constant)
Sdivisor = 3;
end
properties (Abstract)
data
end
properties (Access = private)
a
b
end
methods
function obj = S(Sa, Sb)
obj.a = Sa;
obj.b = Sb;
end
function obj = addSdata(obj)
newb = obj.b / obj.Sdivisor;
newcol = repmat(newb, obj.a, 1);
obj.data = [obj.data newcol];
disp('Object in S Workspace:');
disp(obj);
disp(obj.data);
end
end
end
R.m:
classdef R
properties (Constant)
Rmultiplier = 3;
end
properties (Abstract)
data
end
properties (Access = private)
a
c
end
methods
function obj = R(Ra, Rc)
obj.a = Ra;
obj.c = Rc;
end
function obj = addRdata(obj)
newc = obj.Rmultiplier * obj.c;
newcol = repmat(newc, obj.a, 1);
obj.data = [obj.data newcol];
disp('Object in R Workspace:');
disp(obj);
disp(obj.data);
end
end
end
testM.m:
Output:
>> TestM
Original M Object:
M with properties:
data: [10×2 double]
Sdivisor: 3
Rmultiplier: 3
102 100
102 100
102 100
102 100
102 100
102 100
102 100
102 100
102 100
102 100
Object in S Workspace:
M with properties:
data: [10×3 double]
Sdivisor: 3
Rmultiplier: 3
102 100 34
102 100 34
102 100 34
102 100 34
102 100 34
102 100 34
102 100 34
102 100 34
102 100 34
102 100 34
M Object after calling S Method:
M with properties:
data: [10×2 double]
Sdivisor: 3
Rmultiplier: 3
102 100
102 100
102 100
102 100
102 100
102 100
102 100
102 100
102 100
102 100
M object after calling R Method:
Object in R Workspace:
M with properties:
data: [10×3 double]
Sdivisor: 3
Rmultiplier: 3
102 100 300
102 100 300
102 100 300
102 100 300
102 100 300
102 100 300
102 100 300
102 100 300
102 100 300
102 100 300
M with properties:
data: [10×2 double]
Sdivisor: 3
Rmultiplier: 3
102 100
102 100
102 100
102 100
102 100
102 100
102 100
102 100
102 100
102 100
As you can see, it creates the object correctly and sets up the initial data. Further, when I call the superclass methods, it uses the subclass object, M, and changes it as expected while it is running. So up to that point, it runs as I'm expecting.
Once it exits the method, however, the subclass object returns to its original state. This happens for both superclass methods.
What do I need to change to get this to work the way I want?