I am having trouble understanding the behaviour of Matlab when it comes to changing the properties of objects that are set as a property of some other object. Specifically, suppose I have three objects: a parentObject
, a childObject
and a propertyObject
. Of course, in this simple example, these properties seem a bit nonsensical, but I'm writing quite a large program, for which it is useful to group all corresponding methods and properties on these levels.
Now, my definition of these classes is as shown below. The parentObject
inherits from dynamicprops
to allow it to have an interactively editable list of children. The propertyObject
inherits from dynamicprops
, because in my program, I need to be able to dynamically add (or delete unused) properties.
classdef parentObject < dynamicprops
properties
name
children = [];
end
methods
function obj = parentObject(name)
obj.name = name;
end
function obj = addchild(obj, childName)
obj.children = [obj.children, childObject(childName)];
end
end
end
classdef childObject
properties
name
someProperty(1, 1) propertyObject
end
methods
function obj = childObject(name)
obj.name = name;
end
end
end
classdef propertyObject < dynamicprops
properties
value1 = 0;
value2 = 0;
end
methods
end
end
Now in the following interactive session, I am trying to create children objects, and to adjust one of the default values of one of the childrenObject
's propertyObject
. I'd expect one of the properties, value1
, of one of the children
to change.
However, this is not the case (see below). Changing one of the children's propertyObject
automatically changes the second child's propertyObject
, too. What's more, testing to see whether these two propertyObject
s are equal yields a positive result. I'd expect the instantiation of a child (and thus its default propertyObject
) to yield a unique propertyObject
each time. I'm coming from programming in Python and here this, to the best of my knowledge, is the case. Can anyone explain to me where I'm going wrong and how I can get the classes to behave the way I intend them to?
>> parent = parentObject('parent')
parent =
parentObject with properties:
name: 'parent'
children: []
>> parent.addchild('child1')
ans =
parentObject with properties:
name: 'parent'
children: [1×1 childObject]
>> parent.addchild('child2')
ans =
parentObject with properties:
name: 'parent'
children: [1×2 childObject]
>> parent.children(1).someProperty
ans =
propertyObject with properties:
value1: 0
value2: 0
>> parent.children(2).someProperty
ans =
propertyObject with properties:
value1: 0
value2: 0
>> parent.children(2).someProperty.value1 = NaN
parent =
parentObject with properties:
name: 'parent'
children: [1×2 childObject]
>> parent.children(1).someProperty.value1
ans =
NaN
>> parent.children(1).someProperty == parent.children(2).someProperty
ans =
logical
1
P.s., I see that my question has been marked as a duplicate. However, the duplicate question only answers part of my question; the bit that explains how I can get the desired object behaviour. Even though the duplicate question explains that Matlab loads a default object/property only once, it still isn't clear to me why Matlab, upon actively changing this default value for one instance, suddenly changes the property value for all instances. That is, if we cannot change the default value of one instance without changing the value of all instances, what is the purpose of defaulting?