如何在MATLAB中为指向对象的属性指定指针?

时间:2015-11-05 11:54:54

标签: matlab oop pointers

我想创建一个面向对象的有限元程序。我有一个节点类。由于有限元网格中的节点(由类网格表示)是不同的,因此我将节点类创建为值类。当我从类节点实例化对象数组时,我将该对象数组分配给网格的节点属性。我也有一个元素类,代表一个有限元素。我还从这个类创建一个对象数组,并将其分配给 Mesh 的元素属性。到目前为止很清楚。

由于有限元节点也属于元素,我想将一些节点分配给适当的元素。但复制节点会导致数据冗余,因此我想指定 Node 对象的指针,以便 Element localNodes 属性包含一个数组指向特定节点的指针。我应该如何修改下面的课程来实现它?

节点类:

<div class="outer-wrapper">
  <div class="container">
    <div class="left">
      <span>Menu</span>
    </div>
    <div class="right">
      <span><input type="checkbox" checked autocomplete="off"> Checkbox1</span><br>
      <span><input type="checkbox" checked autocomplete="off"> Checkbox2</span><br>
      <span><input type="checkbox" checked autocomplete="off"> Checkbox3</span>
    </div>
  </div>
</div>

元素类:

classdef Node

   properties
      coordinate;
   end

   methods
     % Not interesting for this example
   end

end

网格类:

classdef Element

   properties
      localNodes; % the object instantiated from the class Element
                  % will store an array of pointers to the
                  % appropriate elements of the object array stored
                  % in Mesh.nodes. How can I assign these pointers
                  % to Element.localNodes?
   end

   methods
     % Not interesting for this example
   end

end

2 个答案:

答案 0 :(得分:3)

最后,在这里进行一些讨论后,我将如何解决这个问题:

classdef Node < handle

   properties
      coordinate;
   end

   methods
       function obj=Node(id)
           obj.coordinate=id;
       end
   end

end

classdef Mesh < handle

   properties
      nodes;    % its object will contain an object array of Node
      elements; % its object will contain an object array of Element
   end

   methods
       function obj=Mesh(nodes,elements)
           pnodes=cell(1,nodes);
           for idx=1:nodes
               pnodes{idx}=Node(idx);
           end
           obj.nodes=[pnodes{:}];
           pelements=cell(1,numel(elements));
           for idx=1:numel(elements)
               pelements{idx}=Element(obj.nodes(elements{idx}));
           end
           obj.elements=[pelements{:}];
       end
       function non_deleted_nodes=get.nodes(obj)
           %getter to return only not-deleted nodes
           obj.nodes=obj.nodes(arrayfun(@isvalid,(obj.nodes)));
           non_deleted_nodes=obj.nodes;
       end
       function non_deleted_nodes=get.elements(obj)
           %getter to return only not-deleted nodes
           obj.elements=obj.elements(arrayfun(@isvalid,(obj.elements)));
           non_deleted_nodes=obj.elements;
       end

   end

end

classdef Element < handle

   properties
      localNodes; % the object instantiated from the class Element
                  % will store an array of pointers to the
                  % appropriate elements of the object array stored
                  % in Mesh.nodes. How can I assign these pointers
                  % to Element.localNodes?
   end

   methods
       function obj=Element(localNodes)
           obj.localNodes=localNodes;
       end
       function non_deleted_nodes=get.localNodes(obj)
           %getter to return only not-deleted nodes
           obj.localNodes=obj.localNodes(arrayfun(@isvalid,(obj.localNodes)));
           non_deleted_nodes=obj.localNodes
       end
       function delete(obj)
           for ix=1:numel(obj.localNodes)
               %The 1 is not a typo, we will delete always the first
               %element until the list is empty
               obj.localNodes(1).delete();
           end
           delete@handle(obj);
       end
   end

end

最后一个简短的演示:

m=Mesh(10,{[1,2],[2,3],[3,4]})
m.elements(1).localNodes
m.elements(1).localNodes(1).delete()
%now the node is deleted from the element and the mesh
m.elements(2).delete()
%now element 2 together with the nodes is deleted.

答案 1 :(得分:1)

您必须使用超类handle,这样才能使用引用而不是复制数据。

from itertools import product

dic = {}
for x, y, z in product(range(200), range(150), range(20)):
    dic[x, y, z] = [self.images[j].GetScalarComponentAsDouble(x, y, z, 0)
                     for j in range(4)]