How to trigger an App Designer UI component's callbacks programatically, like `notify()`
Afficher commentaires plus anciens
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
Réponses (1)
Sean de Wolski
le 31 Août 2021
0 votes
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 commentaires
Tianyu Liu
le 31 Août 2021
Modifié(e) : Tianyu Liu
le 31 Août 2021
Sean de Wolski
le 31 Août 2021
Write the function of the reused interface and have it take the inputs it needs. Call it directly. In the callback, unpack eventdata and pass along just what you need. Something like this:
function buttonCallback(app, event)
reusablefunctionality(event.Value)
end
function callReusableFunctionality()
reusableFunctionality(pi)
end
function reusableFunctionality(x)
sin(x)
end
Bruno Luong
le 31 Août 2021
Modifié(e) : Bruno Luong
le 31 Août 2021
What the intended usecase for notify()? I want to do the same thing see this thread that remains unanswered.
The reason: I want to trigger the callback from an independent thread. If I call the function directly MATLAB crashes because it's not thread safe. So calling directly the callback is NOT an option.
Tianyu Liu
le 1 Sep 2021
Modifié(e) : Tianyu Liu
le 1 Sep 2021
Bruno Luong
le 1 Sep 2021
Not sure about your comment. My code I posted earlier in other thread is programmatically creates an uibutton, it has nothing to do with App Designer.
Tianyu Liu
le 1 Sep 2021
Bruno Luong
le 1 Sep 2021
Modifié(e) : Bruno Luong
le 1 Sep 2021
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.
Catégories
En savoir plus sur Develop Apps Using App Designer dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!