How to find parent uifigure for a child several layers down

16 ビュー (過去 30 日間)
Otis
Otis 2022 年 9 月 23 日
コメント済み: Otis 2022 年 9 月 23 日
I want to call uiconfirm from a uilistbox's ValueChangedFcn. uiconfirm requires a uifigure as its first argument, but the uilistbox is several layers down from the parent uifigure. These layers consist of custom components (built from matlab.ui.componentcontainer.ComponentContainer) and uigridlayouts. Is there a builtin way to find the handle to the parent uifigure? I want to avoid having to pass the figure handle down through each layer. I'm sure that this question has been asked and answered before, but I've been unable to find the solution
Thanks times a million,
Bill

採用された回答

Steven Lord
Steven Lord 2022 年 9 月 23 日
Use ancestor.
>> f = uifigure;
>> h = uipanel('parent', f);
>> f2 = ancestor(h, 'matlab.ui.Figure');
>> f == f2
ans =
logical
1
  1 件のコメント
Otis
Otis 2022 年 9 月 23 日
Awesome!! Just what I was looking for. I think I've seen this before, so I don't know why it didn't come up in any of my searches.
Thanks times mutiple millions :)
Bill

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

その他の回答 (1 件)

dpb
dpb 2022 年 9 月 23 日
" I want to avoid having to pass the figure handle down through each layer."
In an app, data are all global anyway, so save the uifigure handle on startup/creation and use the saved variable value/handle instead.
  3 件のコメント
dpb
dpb 2022 年 9 月 23 日
編集済み: dpb 2022 年 9 月 23 日
The app struct is global, yes...otherwise you wouldn't have access to all its members from any function.
@Steven Lord's solution is better coding practice, though, agreed -- I've never run across ancestor in 30+ years w/ MATLAB, either...as you say, it certainly is not well cross-linked into the documentation to make one aware of it. It isn't in the "See Also" list with findobj although findobj is linked to in ancestor. It is in the "Functions" list for findobj although didn't make the hit parade...
Otis
Otis 2022 年 9 月 23 日
Got it ... Thanks!

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

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by