Save checked items from uitree in a cell array
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!