I create my own class as below:
classdef testClass < handle properties value; map = containers.Map('KeyType','double','ValueType','any'); endend
My goal is for each object of testClass to maintain its own map. However, it turns out that there is only one map object that for the whole class: all objects of testClass access to the same containers.Map. For example, if I create two objects as follows
a = testClass;b = testClass;a.value = 'a';b.value = 'b';a.map(1) = 123;b.map(2) = 321;
It ends up both a and b's map contains two key-value pairs:
>> aa = testClass handle Properties: value: 'a' map: [2x1 containers.Map]>> bb = testClass handle Properties: value: 'b' map: [2x1 containers.Map] Methods, Events, Superclasses
Both (key,value) pairs (1,123) and (2,321) appears in both a.map and b.map
>> a.map.keysans = [1] [2]>> a.map.valuesans = [123] [321]>> b.map.keysans = [1] [2]>> b.map.valuesans = [123] [321]
Is this a bug? How can I keep independent containers.Map for each class object??
Best Answer