Initializing class property values outside of constructor

5 views (last 30 days)
Here is a very simple class definition:
classdef DummyClass < handle
properties
myMap = containers.Map('KeyType','uint32','ValueType','uint32');
end
methods
end
end
Now, here is a very strange series of commands using this class:
>> d = DummyClass();
>> d.myMap(1) = 1;
>> clear;
>> d = DummyClass();
>> d
d =
DummyClass with properties:
myMap: [1x1 containers.Map]
>> d.myMap
ans =
Map with properties:
Count: 1
KeyType: uint32
ValueType: uint32
Why does the value of the myMap property persist across calls to clear !?!
I have noticed that when I move the initialization of the myMap property into the class constructor this does not happen. I already have several fixes for this 'problem' so I am not looking for a solution here - instead I am looking for an explanation.
Thank you.

Answers (2)

per isakson
per isakson on 19 Feb 2014
Edited: per isakson on 19 Feb 2014
Because Matlab behaves that way;-)
clear doesn't clear 'containers.Map', but clear classes does - according to inmem.
[~,~,C] = inmem; C
d = DummyClass();
clear
[~,~,C] = inmem; C
clear classes
[~,~,C] = inmem; C
returns
C =
'com.mathworks.mlwidgets.workspace.WhosInformation'
C =
'com.mathworks.mlwidgets.workspace.WhosInformation'
'containers.Map'
C =
Empty cell array: 0-by-1
.
Later
inmem
d = DummyClass();
d.myMap(1) = 117;
clear
d = DummyClass();
d
d.myMap(1)
returns
ans =
'workspacefunc'
'onCleanup'
'imformats'
d =
DummyClass with properties:
myMap: [1x1 containers.Map]
ans =
117
and
inmem
d = DummyClass();
d.myMap(1) = 117;
clear classes
d = DummyClass();
d
d.myMap(1)
returns
ans =
'workspacefunc'
'onCleanup'
'imformats'
d =
DummyClass with properties:
myMap: [0x1 containers.Map]
Error using containers.Map/subsref
The specified key is not present in this container.
  3 Comments
per isakson
per isakson on 19 Feb 2014
Edited: per isakson on 19 Feb 2014
Matlab is not open source and we cannot know WHY.
In the examples, which I added to my answer, clear does not behave as expected, i.e. as I interpret the documentation. However, clear classes does!
Whether this is an issue, a bug or a mistake in the documentation, I can only guess. (Or I have missed something in the documentation. That happens.)
KARL BEUTNER
KARL BEUTNER on 19 Feb 2014
Edited: KARL BEUTNER on 19 Feb 2014
Just because Matlab is not open source does not mean I cannot know WHY something was implemented or designed in a certain way, it simply keeps me from knowing HOW it was implemented.
I don't care how Matlab has implemented its' classes, I simply want to know why they thought this would be a good feature to have. It seems to me that in this particular case, the MathWorks team may be trying to allow for something like a static class property but have failed badly.
Alternatively, this is truly a bug.

Sign in to comment.


per isakson
per isakson on 19 Feb 2014
Edited: per isakson on 21 Feb 2014
myMap is the default map of the class DummyClass. The value of myMap is a containers.Map. That map is share among all instances of DummyClass, whether or not it holds data. That seems to be by Design.
>> d = DummyClass
d =
DummyClass with properties:
myMap: [0x1 containers.Map]
>> d.myMap(1)=117;
>> e = DummyClass
e =
DummyClass with properties:
myMap: [1x1 containers.Map]
>> e.myMap(1)
ans =
117
[...]Default Values
Classes can define default values for properties. A default value is defined by the class and each instance of the class is assigned that default value. Default values can be used when there is a constant default that can be documented with the class definition. Default values can save space in MAT-Files because defaults are saved once per class and values of properties not differing from default values don’t need to be saved. Generally speaking default values should be literal values wherever possible so that it is clear what the default is.
.
Later
[...] Note: Evaluation of property default values occurs only when the value is first needed, and only once when MATLAB first initializes the class. MATLAB does not reevaluate the expression each time you create a class instance.

Products

Community Treasure Hunt

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

Start Hunting!