Saving System Objects With Children System Objects
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Say that I have two system objects, x and y, that are from classes obj1 and obj2, respectively. Also, obj1 is a child to obj2.
x = obj1;
y = obj2('Prop1',obj1); %Obj1 is a child to Obj2
If I change a property of x, the it changes for y as well.
x.propA = 10; % Now, y.Prop1.propA is 10 as well
The reason is that system objects are also handle objects, so x and y are really pointers to objects. Thus, to save and load the variables, I need to include the following code in my class definitions for obj2:
methods (Access = protected)
function s = saveObjectImpl(obj)
s.Prop1 = matlab.System.saveObject(obj.Prop1);
end
function loadObjectImpl(obj,s,wasInUse)
obj.Prop1 = matlab.System.loadObject(s.Prop1);
end
end
However, now if I load my workspace and then chagnge x, it no longer changes y!
load my_workspace
x.propA = 20; %x.propA =20, but y.Prop1.propA still is 10
In other words, the pointer link is broken and really two separate objects are created during the load. Is there a way to fix this?
0 Commenti
Risposte (0)
Vedere anche
Categorie
Scopri di più su Create System Objects in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!