How to programatically select nodes of an uitree in AppDesigner?

76 views (last 30 days)
Hi,
I would like to programatically select nodes of a an uitree in AppDesigner. I have
And would like to select all nodes with the Tag "Image". (I alreade have set this Tag during creating the tree).

Accepted Answer

Kevin Holly
Kevin Holly on 22 Jul 2022
Edited: Kevin Holly on 22 Jul 2022
Ron,
Please see the app attached.
The callback in attach app is:
function ButtonPushed(app, event)
app.Node.Tag = 'Image';
app.Node4.Tag = 'Image';
app.Node_3.Tag = 'Image';
for ii = 1:length(app.Tree.Children)
% if strcmp(app.Tree.Children(ii).Tag,'Image')
% app.Tree.CheckedNodes = [app.Tree.CheckedNodes; app.Tree.Children(ii)];
% end
for jj = 1:length(app.Tree.Children(ii).Children)
if strcmp(app.Tree.Children(ii).Children(jj).Tag,'Image')
app.Tree.CheckedNodes = [app.Tree.CheckedNodes; app.Tree.Children(ii).Children(jj)];
end
for kk = 1:length(app.Tree.Children(ii).Children(jj).Children)
if strcmp(app.Tree.Children(ii).Children(jj).Children(kk).Tag,'Image')
app.Tree.CheckedNodes = [app.Tree.CheckedNodes; app.Tree.Children(ii).Children(jj).Children(kk)];
end
end
end
end
end
If you wanted to just select 2nd layer of nodes, you could do the folllowing:
for ii = 1:length(app.Tree.Children)
for jj = 1:length(app.Tree.Children(ii).Children)
if strcmp(app.Tree.Children(ii).Children(jj).Tag,'Image')
app.Tree.CheckedNodes = [app.Tree.CheckedNodes; app.Tree.Children(ii).Children(jj)];
end
end
end
  1 Comment
Ron Hoebe
Ron Hoebe on 22 Jul 2022
Edited: Ron Hoebe on 22 Jul 2022
Dear Kevin,
Thanks, with a little modification it works. I wanted to select them, (not check them). The code below is perfect for me.
Best, Ron
app.Tree.SelectedNodes=[];
for ii = 1:length(app.Tree.Children)
for jj = 1:length(app.Tree.Children(ii).Children)
if strcmp(app.Tree.Children(ii).Children(jj).Tag,'Image')
app.Tree.SelectedNodes = [app.Tree.SelectedNodes; app.Tree.Children(ii).Children(jj)];
end
for kk = 1:length(app.Tree.Children(ii).Children(jj).Children)
if strcmp(app.Tree.Children(ii).Children(jj).Children(kk).Tag,'Image')
app.Tree.SelectedNodes = [app.Tree.SelectedNodes; app.Tree.Children(ii).Children(jj).Children(kk)];
end
end
end
end

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 22 Jul 2022
You can use app.Tree.SelectedNodes
I found this example helpful. There will need to be some modifications to get it to work inside an app. You could use the names of the nodes to select, but that gets cumbersome with the names you use.
app.Tree.SelectedNodes = [app.C1tifNode]
I find it easier to use the tree structure to select multiple nodes (note you must turn Multiselect on. Select the tree in your component browser and expand the interactivity property). For example, this code selects the 2nd and 3rd child nodes under the 2nd node.
app.Tree.SelectedNodes = app.Tree.Children(2).Children(2:3);
For your final question on using the Tag property to select nodes, the only way I could find to do this was to loop through each node. I don't love this solution, but it works.
nodes = [];
for n = 1:length(app.Tree.Children)
for n1 = 1:length(app.Tree.Children(n).Children)
if strcmp(app.Tree.Children(n).Children(n1).Tag,'image')
nodes = [nodes app.Tree.Children(n).Children(n1)];
end
end
end
app.Tree.SelectedNodes = nodes
  2 Comments
Cris LaPierre
Cris LaPierre on 22 Jul 2022
I see you got an answer while I was working on mine. I'll leave it in case it is helpful.
Ron Hoebe
Ron Hoebe on 22 Jul 2022
Hi Cris,
Thanks for alle the examples. The TAG Property is very handy for me to select nodes. So Kevin's answer and your last example are perfect for me.
Best,Ron

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps 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!