How to copy uitree content in AppDesigner from app.uitree_1 to app.uitree_2?

8 次查看(过去 30 天)
Hello,
is there any simple (or complex) way to copy THE FULL TREE from object A (app.uitree_A) to object B (app.uitree_B)?
Seems like a reasonable feature to have, doesn't it?
Thank You!

回答(1 个)

Benjamin Turner
Benjamin Turner 2023-2-6
I just quickly put something together:
I created one function to find the parent treeNode:
function [Node] = searchParentNode(Node)
Node = Node.Parent;
if ~strcmp(Node.Type,"uitreenode")
return
end
Node=searchParentNode(Node);
end
And then a function to recursively copy the nodes
function createRecursiveNodes(Parent, Node)
for n = Node.Children'
p = uitreenode(Parent, Text=n.Text, NodeData=n.NodeData, Tag=n.Tag, Icon=n.Icon);
createRecursiveNodes(p, n);
end
end
Here an example:
tree = uitree(); %create a tree
%create some example nodes
parent = uitreenode(tree,Text='AAA');
titles = 'abc';
for i = 1:3
uitreenode(parent,Text=titles(i), NodeData=rand(1))
end
parent = uitreenode(tree,Text='BBB');
titles = 'dce';
for i = 1:3
uitreenode(parent,Text=titles(i), NodeData=rand(1))
end
TreeNodes = tree.CheckedNodes;
PNode= searchParentNode(TreeNodes(1)); %just pick any of the selected nodes and search for the UITree parent.
You can of course put as well the tree directly (it just wasnt possible in my case):
PNode = tree
then copy everything:
tree2 = uitree()
app.createRecursiveNodes(tree2,PNode); % Copy all Children recursively Top-to bottom

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by