Saving System Objects With Children System Objects
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Create System Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!