Bug, feature, or misuse? Issue with map container

5 Ansichten (letzte 30 Tage)
Jason
Jason am 27 Feb. 2013
Kommentiert: P Lepage am 29 Okt. 2020
I'm using Matlab 2012a and I'm trying out map containers and I came across what I think is a bug. I have a variable which is a structure and one of the elements in this structure is a map container. I want to make a copy of the map container and make it a new element in the structure. I then update a value on one of the container values and I find that both containers are updated with the new value.
For example:
X = 10;
Test.Obj1 = containers.Map({'X'},{X});
when I examine the contents, Test.Obj1('X'), I get 10. Now I make a new container and set it equal to the Obj1 container
Obj2 = Test.Obj1;
Now change the value in this new container
Obj2('X') = 20;
Now if I examine Test.Obj1('X') it now equals 20 instead of 10.
Is this a bug or am I misunderstanding containers?
Thanks!
  3 Kommentare
Jason
Jason am 27 Feb. 2013
Ok, the solution I came up with was to create a function that creates a the container and when call that function to create the "copy".
Not sure if there's a better solution out there so would welcome comments.
Thanks!
Honglei Chen
Honglei Chen am 27 Feb. 2013
I don't know what's in your function but I have an example below in my answer.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Honglei Chen
Honglei Chen am 27 Feb. 2013
Here is an example
h1 = containers.Map({'X','Y'},{'X','Y'})
h2 = containers.Map(h1.keys,h1.values)
h2('Z') = 'Z'
h1
HTH
  1 Kommentar
P Lepage
P Lepage am 29 Okt. 2020
It's crazy that there is no official feature to do that, but can manage to find a workaround.
I have data structure made of map<char, map<... etc>>. Since maps stays handles when put in other maps, the solution you provided does not work.
One day I was experimenting with Data Dictionaries (Simulink) and found that the wrapper class Entry returns a deep clone of the object it holds when you call getEntry.
I ended up writing a function that uses this accidental feature to clone my data structure, but it's to slow to be used in a program.
Conclusion : Matlab can break deep references to handle objects, but it wont let you.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by