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

126 ビュー (過去 30 日間)
dzida
dzida 2018 年 6 月 2 日
編集済み: Adam Danz 2023 年 2 月 12 日
How to get handle from currently running app made in App Designer? I would like to use it in a matlab function.
  3 件のコメント
Rodney Case
Rodney Case 2020 年 9 月 11 日
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 2020 年 9 月 11 日
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 ;-)

サインインしてコメントする。

回答 (3 件)

Adam Danz
Adam Danz 2020 年 9 月 11 日
編集済み: Adam Danz 2023 年 2 月 12 日
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 件のコメント
Walter Roberson
Walter Roberson 2023 年 2 月 2 日
"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 2023 年 2 月 7 日
編集済み: Raph 2023 年 2 月 7 日
@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.

サインインしてコメントする。


Wadaane Mohamed
Wadaane Mohamed 2018 年 11 月 25 日
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 件のコメント
Adam Danz
Adam Danz 2020 年 4 月 7 日
編集済み: Adam Danz 2020 年 4 月 27 日
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

サインインしてコメントする。


Raph
Raph 2023 年 2 月 7 日
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

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by