How to trigger an App Designer UI component's callbacks programatically, like `notify()`

42 ビュー (過去 30 日間)
Tianyu Liu
Tianyu Liu 2021 年 8 月 31 日
編集済み: Bruno Luong 2021 年 9 月 1 日
Assume there is a uibutton in an App Designer uifigure referenced as app.Button, and I'd like to trigger its callbacks programatically. There seems to a function called notify(), however, it doesn't seem to work with uifigure elements, maybe because of the NotifyAccess. For exampmle, if I:
notify(app.Button,'ButtonPushed')
will give an error:
Error using matlab.ui.control.Button/notify
Cannot notify listeners of event 'ButtonPushed' in class 'matlab.ui.control.Button'.
I wonder if there is some method to trigger the callbacks programatically, instead of actually calling the defined callbacks function with a fabricated EventData or struct?
Cheers

回答 (1 件)

Sean de Wolski
Sean de Wolski 2021 年 8 月 31 日
Do you want to do this in the scope of testing, or to reuse the functionality of the button callback? For the former, look at matlab.uitest.TestCase.press(). For the latter, put whatever functionality you want in another function and just call it directly.
  7 件のコメント
Tianyu Liu
Tianyu Liu 2021 年 9 月 1 日
@Bruno Luong Oh yes, it doesn't work. BTW, should your code be notify(btn, 'PropertyAdded') instead of fig? Although it is still not allowed, of course.
Bruno Luong
Bruno Luong 2021 年 9 月 1 日
編集済み: Bruno Luong 2021 年 9 月 1 日
Possibly I read from the doc listener events are broadcast, not sure if it arrives automatically to children. In any case the same errors occurs with btn.

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

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by