Change the check box value of a Matlab Tree Node Check Box via code

28 views (last 30 days)
Dear mathworks community,
I am currently developing a Matlab GUI and my main element is a Matlab Tree (Check Box) in the Matlab App Designer.
As there are potentially more than 30 Nodes aavalaible to select, I want to add a "SELECT ALL" check box to the gui, that changes the value of all check boxes of the Tree Node to checked or unchecked. Is there a way to do so? Is there a command to change the value of the check box of the node?
If there is no easy way to do it, can you imagine a workaround?
Thanks in advance for your help!

Accepted Answer

Cameron
Cameron on 14 Feb 2023
Can you not make them second level nodes?
fig = uifigure;
cbt = uitree(fig,'checkbox','Position',[20 20 150 150]);
% First level nodes
category1 = uitreenode(cbt,'Text','All','NodeData',[]);
% Second level nodes.
p1 = uitreenode(category1,'Text','Option 1');
p2 = uitreenode(category1,'Text','Option 2');
p3 = uitreenode(category1,'Text','Option 3');
p4 = uitreenode(category1,'Text','Option 4');
p5 = uitreenode(category1,'Text','Option 5');
  3 Comments
Cameron
Cameron on 14 Feb 2023
fig = uifigure;
cbt = uitree(fig,'checkbox','Position',[20 20 150 150]);
% Second level nodes.
p1 = uitreenode(cbt,'Text','Option 1');
p2 = uitreenode(cbt,'Text','Option 2');
p3 = uitreenode(cbt,'Text','Option 3');
p4 = uitreenode(cbt,'Text','Option 4');
p5 = uitreenode(cbt,'Text','Option 5');
%if you don't run this next line, none of the boxes will be checked.
cbt.CheckedNodes = cbt.Children;
Are Mjaavatten
Are Mjaavatten on 16 Jan 2024
Edited: Are Mjaavatten on 16 Jan 2024
I needed to start with all boxes checked. This function does this for two levels.
function checkbox_tree_check_all(tree)
topNodes = tree.Children;
allNodes = topNodes;
for i = 1:numel(topNodes)
allNodes = [allNodes;topNodes(i).Children];
end
tree.CheckedNodes = allNodes;
end
Are there more elegant ways?

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!