Get logical values from checked TreeNodes

52 ビュー (過去 30 日間)
Daniel Dickinson
Daniel Dickinson 2022 年 10 月 26 日
コメント済み: Jasmine Poppick 2024 年 1 月 25 日
I have made a Checkbox Tree Node as part of an app I am designing. The number of nodes may vary depending on the data fed to the app, so the nodes are created using
for a = 1:someNumber
node(a) = uitreenode(app.Tree,'Text',['Node' num2str(a)] );
end
Now I have a list of nodes. Based on user input, some are checked and some are unchecked. I want to get a logical array that represents which boxes are checked. So suppose I have 5 nodes, and nodes 2, 3 and 5 are checked. I want a one-line statement that returns
[0 1 1 0 1]
but
app.Tree.CheckedNodes
returns TreeNode objects that seem to just contain the names of the nodes. I could write some complicated search function to match the names to the list of all node names, but this seems silly. Is there an easier way? I'm open to using something other than a CheckBox Tree Node if it would be more efficient.

採用された回答

Jasmine Poppick
Jasmine Poppick 2022 年 10 月 27 日
You can do this using ismember to query which nodes are in the CheckedNodes array:
allnodes = findall(app.Tree,"Type","uitreenode");
ismember(allnodes,app.Tree.CheckedNodes)
  3 件のコメント
Laura Ferreira
Laura Ferreira 2024 年 1 月 23 日
what if i want the text? I want force the check of a certain node based on its text. Instead of an array of tree node objects i would like an array of the text propreties. Can you help? Thank you
Jasmine Poppick
Jasmine Poppick 2024 年 1 月 25 日
You can get a cell array of the text of all of the nodes by querying the Text property:
nodes = findall(app.Tree,"Type","uitreenode");
nodeText = {nodes.Text};
However, if you want to programmatically check a node with specific text, it might be easier to find that node directly in the call to findall:
matchingNode = findall(app.Tree,"Type","uitreenode","Text","MyNodeText");
app.Tree.CheckedNodes = matchingNode;

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInstall Products についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by