Calling App designer function from .m script

25 views (last 30 days)
Hello guys,
first to describe the situation: I have GUI made in App Designer and Matlab .m script. These two are working together and trading variables without issues.
My problem is that I would like to call function which is in AppDesigner from .m script file.
Situation:
From GUI user presses button which will initiate communication to actuator.
This communication will be then processed by the .m script
I would then like to call function in AppDesigner which would take the received data from script and use them.
I was not able to find this problem anywhere, I only found it solved from other direction (calling script/function from AppDesigner)
I did try this:
gui = GUI;
gui.ReadErrorButton.ButtonPushedFcn(); -> ERROR: Unrecognized method, property, or field 'ReadErrorButton' for class 'GUI'.
I get this information when I check for the function during debugging:
val =
@(source,event)tryCallback(appdesigner.internal.service.AppManagementService.instance(),app,callback,requiresEventData,event)
Any help appreciated, thank you,
David

Accepted Answer

TADA
TADA on 9 Sep 2021
App designer GUIs are not a separate application, App Designer simply generates matlab classes and wraps them in an mlapp file for some project management reason or whatever (obscurity in my opinion, but nevermind that).
as such, you can do with AppDes GUI objects whatever you can do with any other matlab handle object, under some restrictions, since the code isn't exacly editable (for a good reason, still).
So you can calll GUI properties and functions from matlab script and vice versa, there should be no problem with that.
Notice that the error particularly mentions the button doesn't exist
"ERROR: Unrecognized method, property, or field 'ReadErrorButton' for class 'GUI'."
my guess is you named it something different and therefore, cannot access this gui.ReadErrorButton because it actually doesn't exist.
Once you resolve this, you should be able to call the callback function via the ButtonPushedFcn property of the button, but you will also need to specify the two callback parameters (src, eventData)
gui.ReadErrorButton.ButtonPushedFcn(gui.ReadErrorButton, event.EventData);
again, if you use the real name of the button, the above invocation of the callback function should work.
to change the button name, you can find the button in the Component Browser when you open your app in App Designer,
when you find it, right click it, and rename it to "ReadErrorButton" (or left click it in the component browser and click F12)
  2 Comments
TADA
TADA on 9 Sep 2021
actually this:
gui.ReadErrorButton.ButtonPushedFcn([], []);
might work as well, depending on how you wrote the callback function
David Cervinek
David Cervinek on 12 Sep 2021
Helo TADA,
thanks for feedback, it does work...it seems that I made a typo as well as put wrong parameters in brackets.
Once again, thanks and have a nice day,
David

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!