MATLAB Answers

Replace all pointer to handle object by pointer to a different handle object

조회 수: 7(최근 30일)
Pablo Noever
Pablo Noever 2019년 1월 4일
댓글: Pablo Noever 2019년 1월 7일
Dear all,
I am struggling to solve a problem concerning handle objects. Let me first explain my object constallations. I have three different classes:
Node class:
classdef Node < handle
properties
X %double
Y %double
Z %double
end
end
Element class:
classdef Element < handle
properties
Nodes % Node objects (1x4)
end
end
Mesh class:
classdef Element < handle
properties
Elements % Element objects (1xN)
Nodes % List of all nodes in Element
end
end
The is an abstraction of my problem. Now I want to merge two Nodes ( Node1 into Node 2).
The Idea is either :
  1. To change all Pointer of the handle object Node1 to Pointer to Node2
  2. Change 'root object' of Node1 (where the pointer references to) to the 'root object' of Node2, so that the pointer redirect all?!
  3. Delete Node1 and during deletion replace by a pointer to Node2
The problems:
  1. I didn't find any method to get all Pointer to Node1
  2. How can I acces this 'root object'
  3. No idea if and how it is possible
I have tried:
classdef Node < handle
properties
X %double
Y %double
Z %double
end
methods
function merge2(obj,NewNode)
obj = NewNode;
end
end
end
% Call
Node1.merge2(Node2)
But this only changes the handle variable obj and does not change redirect the 'root object' from Node1 to Node2.
Does anyone have a good solution for this, without looping over all properties to find all pointer and manually change the handle variable?
Thanks a lot guys!
Best regards.

채택된 답변

Philip Borghesani
Philip Borghesani 2019년 1월 4일
How about one extra level of indirection?:
classdef node < handle
properties
X
Y
Z
end
end
classdef nodeHandle < handle
properties
node
end
methods
function merge2(obj,Other)
obj.node=Other.node
end
end
end
Now store nodeHandle in Element and Mesh instead of node. You may find that all your classes don't need to be handles. We have found that in the best designs many object types can still be values.
  댓글 수: 2

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by