defining a list of class instances

5 visualizaciones (últimos 30 días)
Reza Deabae
Reza Deabae el 29 de Sept. de 2022
Comentada: Reza Deabae el 29 de Sept. de 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.

Respuestas (1)

David Hill
David Hill el 29 de Sept. de 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 comentario
Reza Deabae
Reza Deabae el 29 de Sept. de 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?

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by