Looping UI Tree Node

조회 수: 10 (최근 30일)
Danny Choi
Danny Choi 2018년 6월 26일
답변: Ayush Singh 2024년 7월 17일
I am currently using a mytreeapp function to create uitree.
The thing is all the examples I can find has less than 10 uitreenode which you have to individually define.
(ex. p1 = uitreenode(cateogry1, 'Text','A'..., p2 = uitreenode(category1, 'Text','B'...)
Is there a way I can use for loop or other loop methods to create many uitreenode? Or use other function than mytreeapp to do this?
Thank you!

답변 (1개)

Ayush Singh
Ayush Singh 2024년 7월 17일
Hi Danny,
Yes, you can use a loop to create multiple `uitreenode` objects programmatically. Below is an example of how you can achieve this using a for loop.
Assuming you want to create a tree with a root node and multiple child nodes.
  • Create the `uitree`
First, create the `uitree` in your figure or app.
% Create a figure
fig = uifigure('Name', 'Tree Example');
% Create a tree in the figure
tree = uitree(fig, 'Position', [20, 20, 150, 200]);
  • Create the Root Node
Create the root node of the tree.
% Create a root node
rootNode = uitreenode(tree, 'Text', 'Root Node');
  • Create Child Nodes Using a Loop
Use a for loop to create multiple child nodes under the root node.
% Number of child nodes to create
numNodes = 10;
% Loop to create child nodes
for i = 1:numNodes
% Create a child node with dynamic text
childNode = uitreenode(rootNode, 'Text', ['Node ' num2str(i)]);
end
By using loops, you can easily create multiple `uitreenode` objects programmatically, making it convenient to handle large trees without manually defining each node. This approach is flexible and can be extended to create more complex tree structures as needed.

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by