Identify the portion of the uitree in CheckedNodes
10 次查看(过去 30 天)
显示 更早的评论
When creating a tree with multiple treenodes, how can one identify the specific node that a checked item comes from?
Given the tree generated from the sample code in matlab (see below), How can I know the parent node for each checked node? Specifically, "train" could be under either vehicle or action in the example. tr.CHeckedNodes appears to return an array without indicating the parent node.
Thanks for the help.
fig = uifigure;
gl = uigridlayout(fig,[1 2]);
gl.ColumnWidth = {'2x','1x'};
vehicle=["train" "train" "plane" "plane" "plane"]';
action=["test" "test" "test" "train" "train"]';
T=table(vehicle, action);
tbl = uitable(gl,"Data",T);
tr = uitree(gl,'checkbox');
vars = string(T.Properties.VariableNames);
for k1 = 1:length(vars)
var = vars{k1};
varnode = uitreenode(tr,"Text",var);
rows = T{:,var};
names = categories(categorical(rows));
for k2 = 1:length(names)
text = names{k2};
uitreenode(varnode,"Text",text);
end
end
% manually check vehicle.train and action.test in tree
>> tr.CheckedNodes
ans =
2×1 TreeNode array:
TreeNode (train)
TreeNode (test)
5 个评论
采纳的回答
Everett Weber
2023-1-19
编辑:Everett Weber
2023-1-24
6 个评论
Sanket
2023-1-23
Hi Everett, thanks for the description of “filter_criteria”.
Since the second dimension determines the number of fields, I believe the following line:
num_vars=size(filter_criteria,1);
Should be replaced with:
num_vars=size(filter_criteria,2);
You can definitely attach an example that uses the functions, if it is not too much to add.
更多回答(1 个)
Rohan
2023-1-19
编辑:Rohan
2023-1-19
The parent information is inherently present in each checked node. Since "tr.CheckedNodes" returns an array of checked nodes, the parent information can be found by accessing the "Parent" property of each node in this array, as shown below:
>> parent = tr.CheckedNodes(2).Parent
parent =
TreeNode (action) with properties:
Text: 'action'
Icon: ''
NodeData: []
Show all properties
Even if "train" is checked under both "vehicle" and "action", the parent of both "train" nodes can be found using the same syntax:
>> tr.CheckedNodes
ans =
2×1 TreeNode array:
TreeNode (train)
TreeNode (train)
>> tr.CheckedNodes(1).Parent
ans =
TreeNode (vehicle) with properties:
Text: 'vehicle'
Icon: ''
NodeData: []
Show all properties
>> tr.CheckedNodes(2).Parent
ans =
TreeNode (action) with properties:
Text: 'action'
Icon: ''
NodeData: []
Show all properties
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!