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

206 views (last 30 days)
How to get handle from currently running app made in App Designer? I would like to use it in a matlab function.
  3 Comments
Rodney Case
Rodney Case on 11 Sep 2020
Matlab Apps run in Figure windows. By default, an app's figure window has a hidden handle and is a child of fig(0).
To find the figure window that contains your app, you can use:
h_fig = findall(0,'Name','<App_Title>'); % Where <App_Title> is the string displayed at the top of the app window.
If you design your app to set the Tag of its figure window upon initialization, you could then find it with:
h_fig = findall(0,'Tag','Your_Tag');
The instance of your app is contained in the RunningAppInstance property of the figure window. You can access it as:
h_app = h_fig.RunningAppInstance;
The properties of h_app will include app level globals you've defined plus all of the controls.
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 (3)

Adam Danz
Adam Danz on 11 Sep 2020
Edited: Adam Danz on 12 Feb 2023
3 ways to access an existing app
In these examples the app's name is "MyApp".
1. Store the app handle when opening the app.
This is the best approach.
app = MyApp;
For more info & demos on this method
2. Find the handle to an existing app figure
The HandleVisibility of UIFigures is set to off by default which makes it a bit difficult to find a UIFigure 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 figure handle visible so it can be found with gcf()
Starting in r2020a, open your app in AppDesigner and set your figure's HandleVisibility to 'on' or 'callback'. This 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.
  7 Comments
Walter Roberson
Walter Roberson on 2 Feb 2023
"app components are children or descendants of the app figure"
However, general app properties are not children or descendants of the app figure. If, for example, you had created a property directory that you store the path of some file into, then finding the handle of the uifigure will not generally help you get access to that directory property of the app. (It is true that getting the handle of the uifigure might help you grab the handle of a callback attached to some child of the uifigure, and that might potentially help you get access perhaps)
Raph
Raph on 7 Feb 2023
Edited: Raph on 7 Feb 2023
@Adam Danz The main reason someone would want the app handle is to run public functions or get public variables. Simply returning the parent figure handle will not allow that. Obviously, its not the 100% case but its the typical use case for this question , imo.

Sign in to comment.


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
Edited: Adam Danz on 27 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.


Raph
Raph on 7 Feb 2023
In appdesigner, simply set the top most UIfigure (not the app) Name in IDENTIFIERS to something not too complex such as 'Login' .
% to find the app object
allfigs = findall(0,'Type', 'figure');
app2Handle = findall(allfigs, 'Name', 'Login');
if numel(app2Handle)>0
app_object = app2Handle(1).RunningAppInstance
% app object is the app handle, you can call public functions or public
% variables from it
end

Categories

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

Community Treasure Hunt

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

Start Hunting!