Filter löschen
Filter löschen

Moving appdesigner tree nodes more efficiently

7 Ansichten (letzte 30 Tage)
Vincent Bocquet
Vincent Bocquet am 3 Nov. 2021
Beantwortet: Sanchari am 27 Apr. 2024
Hello all,
I am working with appdesigner to plot curves and act on the data plotted. I use for that a Tree, with nodes representing data. They come as shown below, by pair of subnodes representing raw/treated data with a common Parent node representing the file.
I wanted to add a "Group" function to allow to regroup nodes in the hierarchy. So I used the code below.
function Group_nodes(app,inputNodes)
nb_Nodes = length(inputNodes);
%Initialization phase
node_to_be_moved = inputNodes(2).Parent;
move(node_to_be_moved, inputNodes(1).Parent);
% Loop on all the inputnodes
for k = 3:nb_Nodes
% Test if parent node has just been moved.
if inputNodes(k).Parent ~= node_to_be_moved
node_to_be_moved = inputNodes(k).Parent;
move(node_to_be_moved, inputNodes(1).Parent);
end
end
What it does is to sweep through the selected nodes and bring them one by one next to the first one. The actual movement is performed on the "Parent" nodes, so I included an if condition to prevent the action from being repeated twice for siblings (Raw/Treated).
The poblem is that the execution time is increasing with the number of iteration in the loop, and can get really high. By measuring the time with tic / toc functions I obtain the following vector with each iteration: (the low values are the cases when the move function is not used as the if condition is not fullfilled).
I don't understand the reason behind this exponential time increase. Is it coming from the move(node1, node 2) function ?
Do you know how to prevent this ?

Antworten (1)

Sanchari
Sanchari am 27 Apr. 2024
Hello Vincent,
The behaviour you're observing with increasing execution time in the loop, especially as the number of iterations grows, is likely due to the way the move function interacts with the tree structure in your app. If your tree has a significant number of nodes, or if the tree's structure is complex, each move operation could become increasingly expensive in terms of computation time since the underlying data structure and the UI need to update to reflect these changes.
To mitigate this issue, consider the following strategies:
1. Bulk Update Mechanism: Instead of moving nodes one by one inside the loop, it might be more efficient to restructure the tree in memory and then apply the changes to the UI in a single operation if the framework allows it. Unfortunately, MATLAB's App Designer and the tree component do not directly offer a bulk update mechanism. However, you can minimize the number of updates by restructuring your approach to how and when you move nodes.
2. Reduce UI updates: One approach to reduce the overhead of UI updates is to temporarily disable UI updates while performing the move operations and then refresh the UI once all moves are completed. For example, Hiding the tree during the update and showing it again after all moves are done.app
Tree.Visible = 'off'; % Hide the tree
% Perform the move operations here
app.Tree.Visible = 'on'; % Show the tree again
3. Optimize the logic: Your current logic checks if a node's parent has already been moved to avoid moving it again. This is a good approach, but the execution time problem suggests that the move operation itself is the bottleneck. Review if there's any way to reduce the number of move operations or to move groups of nodes together rather than individual ones.
4. Consider MATLAB Performance Tips:
  1. Preallocation: Ensure that any data structures you use are preallocated to avoid dynamic resizing inside the loop.
  2. Vectorization: While not directly applicable to moving tree nodes, ensure that other parts of your code that can be optimized are optimized to reduce overall execution time.
5. Debugging Execution Time: To further understand where the time is spent, you can use MATLAB's Profiler. This tool can help you identify which parts of your code are the most time-consuming:
profile on
% Call your function here
profile viewer
You can further refer the following links related to nodes in tree:
  1. Calling parent node in a tree in a App Designer (ML Answer): https://in.mathworks.com/matlabcentral/answers/542003-calling-node-s-parent-node-in-a-tree-in-app-designer?s_tid=answers_rc1-1_p1_Topic
  2. TreeNode Properties (Mathworks Documentation): https://in.mathworks.com/help/matlab/ref/matlab.ui.container.treenode-properties.html?searchHighlight=App%20designer%20tree%20node&s_tid=srchtitle_support_results_4_App%20designer%20tree%20node
  3. App Designer tree node filter (ML Answer): https://in.mathworks.com/support/search.html/answers/1988783-app-designer-problem-implementing-filter-based-on-tree-nodes.html?fq%5B%5D=asset_type_name:answer&fq%5B%5D=category:matlab/app-designer&page=1
Hope this helps!

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by