MATLAB Answers

How to get handle from currently running app made in App Designer?

173 views (last 30 days)
dzida
dzida on 2 Jun 2018
Edited: Adam Danz on 2 Feb 2021
How to get handle from currently running app made in App Designer? I would like to use it in a matlab function.
  3 Comments
Walter Roberson
Walter Roberson on 11 Sep 2020
Thanks, Rodney.
One implication would be that you cannot have a single figure that is running two different Apps. Not sure why you would want to though ;-)

Sign in to comment.

Answers (2)

Adam Danz
Adam Danz on 11 Sep 2020
Edited: Adam Danz on 2 Feb 2021
3 ways to access your app handle
In these examples the app's name is "MyApp".
1. Store the app handle when you open the app.
This is the best approach.
app = MyApp;
For more info & demos on this method
2. Find the handle to an app that's already opened
The HandleVisibility of UIFigures is set to off by default which makes it a bit more difficult to find a UIFigure (or app) handle. The code below finds all figure handles and then isolates the figure belonging to your App.
This assumes you only have 1 figure opened with your App's name. For extra precaution, add a Tag to your App and include the tag in the search.
For instructions on how to name your app, see this more detailed answer.
% get handles to *all* figures
allfigs = findall(0,'Type', 'figure');
% isolate the app's handle based on the App's name.
app2Handle = findall(allfigs, 'Name', 'MyApp'); % MyApp is the App's fig name*
% To include a tag,
app2Handle = findall(allfigs, 'Name', 'MyApp', 'Tag', 'MyUniqueTagName');
3. Make your app handle visible so it can be found with gcf()
Starting in r2020a, open your app in AppDesigner and set your app's HandleVisibility to 'on' or 'callback'. This can can either be done in the App's startup function using MyApp.UIFigure.HandleVisibility='on'; or it can be done in the Design View by selecting your figure handle from the component browser, then set HandleVisibility under the Parent/Child submenu to "on".
Be aware that your App's figure handle will now be available for any plotting function that's looking for the current figure which may lead to undesirable results. This method should be avoided.

Wadaane Mohamed
Wadaane Mohamed on 25 Nov 2018
I solved it !!!
ok, so first declare a global variable in command window :
global myapp
initiate your app, still in command window :
myapp = MyApp;
there's your handle !!
To use it in your script , first mention that you want to access a global variable, then call your functions:
global myapp
myapp.myfunction('Hello World !')
  1 Comment
Adam Danz
Adam Danz on 7 Apr 2020
I wouldn't recommend this approach. There's rarely (never?) a need to work with global variables and they open up a can of worms.
See this answer

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!