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

6 views (last 30 days)
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!

Answers (1)

Benjamin Turner
Benjamin Turner on 6 Feb 2023
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

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!