defining a list of class instances

5 ビュー (過去 30 日間)
Reza Deabae
Reza Deabae 2022 年 9 月 29 日
コメント済み: Reza Deabae 2022 年 9 月 29 日
Hey all,
i am going to define a class in Matlab called "Node" which is sopposed to be a node of a tree stucture.
i have a variable inside it called "NodeList" which is sopposed to be a list of sub nodes.
here is what i have defined:
classdef Node
properties
Title string %name
NodeType Type_Node %enum Design/Variation
NodeList cell{Node} %list of sub nodes
RelationType Relation %enum AND/OR
ImportanceFactor float %float [0 1]
NodeIndex {mustBePositive} %int for drawing the tree
end
methods
function obj=Node(title,nodetype,nodelist,relation,impactfactor,nodeindex)
obj.Title=title;
obj.NodeType=nodetype;
obj.NodeList=nodelist;
obj.RelationType=relation;
obj.ImportanceFactor=impactfactor;
obj.NodeIndex=nodeindex;
end
end
end
while instantiating the class i am getting this error message:
Error setting default value of property 'NodeList' of class 'Node'. The 'Node' class definition uses an instance of itself directly
or indirectly as the default value for the 'NodeList' property. This is not allowed.
first please let me know wether the syntax i have used to define the class is correct or not?
then, i would appreciate any suggestions to make list of instances of a class.
and third, how can i nest a list of instances of the very class inside itself?
your help and comments are highly appreciated.

回答 (1 件)

David Hill
David Hill 2022 年 9 月 29 日
classdef Node
properties
Title
NodeType
NodeList
RelationType
ImportanceFactor
NodeIndex
end
methods
function obj=Node(title,nodetype,nodelist,relation,impactfactor,nodeindex)
obj.Title=title;
obj.NodeType=nodetype;
obj.NodeList=nodelist;
obj.RelationType=relation;
obj.ImportanceFactor=impactfactor;
obj.NodeIndex=nodeindex;
end
end
end
  1 件のコメント
Reza Deabae
Reza Deabae 2022 年 9 月 29 日
Thank you David,
i am trying this right now,
meanwhile let me ask another question,
as i said i have list of Nodes in each node, do you have a suggestion to itterate through all of them?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by