- In the property definition — MATLAB evaluates the expression only once and assigns the same value to the property of every instance.
- In a class constructor — MATLAB evaluates the assignment expression for each instance, which ensures that each instance has a unique value."
Using containers.Map as a Class's property
16 次查看(过去 30 天)
显示 更早的评论
I am having a problem in Matlab while using container.Map as a property of a class. I have a class Sensor with one property which is an object of containers.Map. The problem is, I cannot modify this property for one Sensor object without modifying map in other Sensor object. This property looks to be shared among objects of class Sensor. See below for explanation.
classdef Sensor
properties
id
map=containers.Map;
end
end
s1=Sensor;
s2=Sensor;
s1.map('key1')='value1';
s2.map
At this point s2.map also has same content as s1.map.
Has anyone faced this before or any particular solution. I just changed from table to containers.Map because of the flexibility and speed.
Thanks Saurav
0 个评论
采纳的回答
Steven Lord
2017-3-24
"There are two basic approaches to initializing property values:
You're initializing the property to a containers.Map in the property definition, and additionally a containers.Map has handle semantics. Initialize it instead in the constructor.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!