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

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

It's the latter, "to reuse the functionality of the button callback", but "instead of actually calling the defined callbacks function with a fabricated EventData or struct" and executes exactly like triggered by the pressing the button...
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
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.
@Bruno Luong Yes, I have found your question before posting mine. The purpose of this question/wanting to use notify() was just to be lazy, avoiding extra coding to route around the callbacks when the exact same behaviours are wanted. I think the answer is probably "no"...
Maybe if you setup the UI components' callbacks by addlistener() in script during startup (after the hardcoded initialisation), instead of selecting them from the App Designer UI, then you can notify() them. Because those callbacks set by App Designer are wrapped by createCallbackFcn(), whose NotifyAccess are different.
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.
@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.
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.

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

카테고리

도움말 센터File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 8월 31일

편집:

2021년 9월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by