How to add multiple styles to my uiTree?

조회 수: 11 (최근 30일)
Mark Golberg
Mark Golberg 2022년 1월 7일
댓글: Zhaoyu Gong 2023년 9월 27일
*** using MATLAB 2021b ***
Hello,
I have a simple uiTree, with several nodes. I'd like to update it's nodes (by deleting existing node and creating new one), and each time after node re-creation I'd like to add style to that specific node.
For some reason its working only one time(!). On the second attemp I get the following warning and node color doesn't change.
Warning: Invalid or deleted object.
Strangely enough, if I try to do the same thing, but add style to the entire tree (NOT to a specifc node), then there is no problem at all.
Please see code below:
%% step 1 - create tree
fig = uifigure;
t1 = uitree(fig);
n1 = uitreenode(t1);
n1.Text = 'Node 1';
n2 = uitreenode(n1);
n3 = uitreenode(n1);
n3.Text = 'Node 3';
n2.Text = 'Node 2';
expand(t1 , 'all')
%% step 2 - re-create main node and add style_1 (first delete)
nodeStyle_1 = uistyle;
nodeStyle_1.FontColor = 'red';
% 1st delete - working
n1.delete
n1 = uitreenode(t1);
n1.Text = 'Node 1a';
n2 = uitreenode(n1);
n2.Text = 'Node 2a';
n3 = uitreenode(n1);
n3.Text = 'Node 3a';
expand(t1 , 'all')
addStyle(t1 , nodeStyle_1 , 'node' , n2)
% addStyle(t1 , nodeStyle_1)
%% step 3 - re-create main node and add style_2 (second delete)
nodeStyle_2 = uistyle;
nodeStyle_2.FontColor = 'yellow';
n1.delete
n1 = uitreenode(t1);
n1.Text = 'Node 1b';
n2 = uitreenode(n1);
n2.Text = 'Node 2b';
n3 = uitreenode(n1);
n3.Text = 'Node 3b';
expand(t1 , 'all')
addStyle(t1 , nodeStyle_2 , 'node' , n2)
% addStyle(t1 , nodeStyle_2)
What am I missing here?
* if there is an alternative way to "update" nodes text, without using the "delete" method I'd like to hear about it. I've tried to make it invisible, by:
n1.HandleVisibility = 'off';
but it doesn't do anything...?! (I was expecting the n1 node to disappear...)
THANKS !!!

채택된 답변

Yair Altman
Yair Altman 2022년 1월 9일
  1. This looks like an internal Matlab bug. Specifically, after deleting nodes, the uitree's internal StyleConfigurationStorage property isn't updated, leaving the deleted (invalid) TreeNode handle behind. When a subsequent addStyle tries to access StyleConfigurationStorage, the invalid (deleted) tree node(s) cause an error when they are accessed. The workaround is to remove the uistyle (using the removeStyle() function) before deleting any tree node.
  2. HandleVisibility does NOT control the node visibility - it controls whether or not the handle is visible to functions such as findobj(). See https://www.mathworks.com/help/matlab/creating_plots/preventing-access-to-figures-and-axes.html
  댓글 수: 2
Mark Golberg
Mark Golberg 2022년 1월 9일
Thank you Yair! The removeStyle() works perfectly.
Unfortunately, it it can be applied only for the Tree object, and can not be applied to a specific TreeNode object.
So, basically, if I want to update style of a specific node, I first need to remove the style of the entire tree, update my node, but then also re-apply style to the rest of the tree that didn't participate in that game, right?
Zhaoyu Gong
Zhaoyu Gong 2023년 9월 27일
Just encountered the same problem as you. It is frustrating that removeStyle cannot be applied to node. I personally added a boolean flag in the NodeData to indicate the style. After deleting any node, I reapply the style according to the flag value.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by