callback in TreeNode Propertis

2 ビュー (過去 30 日間)
Luca Re
Luca Re 2024 年 8 月 15 日
コメント済み: Luca Re 2024 年 8 月 16 日
hi, i want to execute calback by click Node2 to execute other function
It's possibile to do it?
  2 件のコメント
Josh
Josh 2024 年 8 月 16 日
Please provide the MATLAB version you are using.
Luca Re
Luca Re 2024 年 8 月 16 日
>> version
ans =
'24.1.0.2603908 (R2024a) Update 3'

サインインしてコメントする。

採用された回答

Tejas
Tejas 2024 年 8 月 16 日
Hello Luca,
Follow the example below to add a callback to a node within a Tree:
  • Define a callback for the Tree using the SelectionChangedFcn property. This property of the Tree component specifies the callback function to execute when the selection changes.
tree.SelectionChangedFcn = @(src, event) nodeSelectionCallback(src, event, node2);
  • Below is an example of the callback code, which checks if node 2 has been selected and, if so, calls another function.
function nodeSelectionCallback(src, event, targetNode)
selectedNode = event.SelectedNodes;
% Check if second node is selected
if isequal(selectedNode, targetNode)
disp('Node 2 has been selected ');
myCustomFunction(); % The other function
end
end
For more details, kindly refer to the documentation below:https://www.mathworks.com/help/matlab/ref/matlab.ui.container.tree-properties.html .
  3 件のコメント
Tejas
Tejas 2024 年 8 月 16 日
Hello Luca,
The method I previously shared is for assigning a callback to a Tree node when it is created in a .M file. While using App Designer, follow these steps to assign a callback to a node:
  • Navigate to Code View, then to Code Browser. Click on the plus icon and add a 'SelectionChangedFcn' callback, as illustrated in the screenshot below.
  • Edit the code within the 'TreeSelectionChanged' function as follows.
% Selection changed function: Tree
function TreeSelectionChanged(app, event)
selectedNodes = app.Tree.SelectedNodes;
if strcmp(selectedNodes.Text,'Node2')
disp('Node2 two has been selected ');
myCustomFunction(); % Call the other function
end
end
  • Ensure that 'myCustomFunction' is a .M file and is present on the MATLAB path.
Luca Re
Luca Re 2024 年 8 月 16 日
thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by