App designer - problem implementing filter based on tree nodes
32 views (last 30 days)
Show older comments
Teshan Rezel
on 27 Jun 2023
Commented: Teshan Rezel
on 10 Jul 2023
Hi folks,
i have a tree that looks something like this:
I am trying to createn a filter based on "cause" whereby you can see things on a table to the left based on one or more of the selected criteria (such as fire, or fire + attack).
So I've used the following code from another thread (below). However, I am getting the error "Unrecognized method, property, or field 'Cause' for class 'matlab.ui.control.Table'. Is there a way around this please?
function [filter_criteria, checked_nodes] = get_checked_nodes(~, checked)
% checked = checked nodes object from tree
% if tr = tree, checked=tr.CheckedNodes
% filter_criteria = structure with variables and values objects,
% each record has the name of the variable in variables as a string and a string array of the values for those items checked in the nodes.
% checked_nodes = table with all checked nodes. Table Structure includes node level (only level 2 is included),
% Parent (variable name), and leaf (selected string option in tree).
% If the parent node is checked, the values will not be returned, because all records would be returned and there are no criteria to filter on.
if size(checked,1)==0
filter_criteria=[];
checked_nodes=[];
return
end
node_num=size(checked,1);
checked_nodes=table(Size=[ node_num 3],VariableTypes=["double" "string" "string"],VariableNames=["node_level" "parent_node" "leaf"]);
for node_id=1:node_num
if checked(node_id).Parent.Type~="uitreenode" % if parent node is not uitreenode, then top level
checked_nodes.node_level(node_id)=1;
checked_nodes.parent_node(node_id)=checked(node_id).Text; % parent nodes have themselves as parent here
checked_nodes.leaf(node_id)=checked(node_id).Text;
else
checked_nodes.node_level(node_id)=2;
checked_nodes.parent_node(node_id)=checked(node_id).Parent.Text;
checked_nodes.leaf(node_id)=checked(node_id).Text;
end
end
check_parents=checked_nodes.leaf(checked_nodes.node_level==1);
node_rows=~ismember(checked_nodes.parent_node,check_parents);
checked_nodes=checked_nodes(node_rows,:); % removes parents from checked_nodes
clear filter_criteria
varnames=unique(checked_nodes.parent_node);
for varnum=1:size(varnames,1)
filter_criteria(varnum).variables=varnames(varnum);
filter_criteria(varnum).values=checked_nodes.leaf(checked_nodes.parent_node==varnames(varnum))';
end
end
function [filtered_rows] = get_filtered_rows(~,source_table, filter_criteria)
% create a logical array of filtered rows based upon a source table and filter criteria.
% The array can then be used to select specific rows from a table or array.
% The filter criteria selects fields that have any option in the values within a field,
% but if multiple fields are used, the row must be selected for each variable used in the filter.
if isempty(filter_criteria)
filtered_rows=true(size(source_table,1),1);
return;
end
num_vars=size(filter_criteria,2);
filtered_matrix=[];
for varnum=1:num_vars
filtered_matrix(:,varnum)=ismember(source_table.(filter_criteria(varnum).variables),filter_criteria(varnum).values);
end
filtered_rows=all(filtered_matrix,2);
end
0 Comments
Accepted Answer
Kevin Holly
on 27 Jun 2023
Teshan,
I created an app based on the scripts provided (see attached). Please let me know if this answers your question. I believe you were inputing the incorrect variable as the source_table into the get_filtered_rows function.
3 Comments
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!