MATLAB: Cross-Talk between handle Class Objects

handles

I've created an abstract super class MySuperClass which is itself derived from matlab.mixin.Copyable. This super class provides a setValue(row, column, value) and a corresponding getValue(row, column) function. From this super class I derived a sub class MyClass whose constructor requires two integers.
By …
a = MyClass(1, 2);
b = MyClass(1, 2);
… I created two objects of that class. isa(a, 'handle') returns logical true – same for b – which confirms that both objects are handle objects.
As…
a == b
… returns (logical) 0, I know that these are independent handles, i.e., independent objects. Right after creation of the objects isequal(a, b) returns logical 1.
Now, something strange happens:
a.setValue(1, 1, 5); % Set first row, first column value of a to 5
b.setValue(1, 1, 3); % Set first row, first column value of b to 3
a.getValue(1, 1)
-> 3
So, there is some "cross talk" between these objects even though they are independent.
Can you please explain why? this happens and how? to get rid of that behaviour. ??? I'm lost.
I'm using Matlab R2016b (9.1.0.441655) on 64-bit Windows.
Kindly, Joschi

Best Answer

This depends entirely on what your setValue and getValue functions do, but taking a flying guess...
If you declare a property that is a handle object and you actually create this within the property block, both objects will have the same object because property block initialisations are evaluated once per class not per object.
So if the thing that is having its value set is some handle class object stored in mySuperClass that you initialised in the property block then this behaviour would occur as they will share a reference to the same actual object.