defining a list of class instances

2 views (last 30 days)
Reza Deabae
Reza Deabae on 29 Sep 2022
Commented: Reza Deabae on 29 Sep 2022
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.

Answers (1)

David Hill
David Hill on 29 Sep 2022
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 Comment
Reza Deabae
Reza Deabae on 29 Sep 2022
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?

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by