Save checked items from uitree in a cell array

16 ビュー (過去 30 日間)
KostasK
KostasK 2021 年 9 月 2 日
編集済み: Bryant Lash 2021 年 9 月 30 日
Hi all,
I have just started using the uitree for a basic app that I am designing. What I would like to do is save the NodeData from each checked item in cell arrays. For example let's say we have the following code.
function mycheckboxtreeapp
fig = uifigure;
cbt = uitree(fig,'checkbox','Position',[20 20 150 150]);
% Assign callbacks in response to node check and selection
cbt.CheckedNodesChangedFcn = @checkchange;
cbt.SelectionChangedFcn = @selectchange;
% First level nodes
category1 = uitreenode(cbt,'Text','Vegetables','NodeData',[]);
category2 = uitreenode(cbt,'Text','Fruits','NodeData',[]);
% Second level nodes.
% Node data is the weight of the food item (in grams)
p1 = uitreenode(category1,'Text','Cucumber','NodeData',400);
p2 = uitreenode(category1,'Text','Carrot','NodeData',65);
p3 = uitreenode(category2,'Text','Apple','NodeData',183);
p4 = uitreenode(category2,'Text','Banana','NodeData',120);
% Expand the tree
expand(cbt);
% Create the function for the CheckedNodesChangedFcn callback
% When this function is executed, it displays the total weight of all checked items
function checkchange(src,event)
nodes = event.LeafCheckedNodes;
if ~isempty(nodes)
data = [nodes.NodeData];
display(sum(data));
end
end
% Create the function for the SelectedNodesChangedFcn callback
% When this function is executed, it displays the name of the selected item
function selectchange(src,event)
node = event.SelectedNodes;
display(node.Text);
end
end
Pretaining to the above, if I was to have all the items checked from the tree, I would like to get the following
{[400 65] [183 120]}
Similarly, if I had everything checked except the last item I would get the following:
{[400 65] [183]}
and so on...
For some reason I cannot accomplish the above. When I call cbt.CheckedNodes.NodeData I get the numbers one by one
Thanks for your help in advance.

採用された回答

Bryant Lash
Bryant Lash 2021 年 9 月 30 日
編集済み: Bryant Lash 2021 年 9 月 30 日
I think the only good way to do this is to manually filter through the parents and do it that way. So under checkChange you'll have to do something like this:
if ~isempty(nodes)
parents = [nodes.Parent];
[uniqueParents,sort1,sort2] = unique(parents);
cellSortedData = cell(max(sort2),1);
for i = 1:size(nodes,1)
cellSortedData{sort2(i),1}(end + 1,1) = nodes(i).NodeData; %Matlab doesn't love the end + 1 but honeslty it's so much easier, and if you have small numbers it literally doesn't matter
end
end
Now this approach will pass your specs, but it'll have to be a little more complex if you need it to always output the same size cellSortedData (for example, if you want to only select the second base node and want the first cell element empty). THat's not too hard either though.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by