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

212 次查看(过去 30 天)
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 !')

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 CenterFile Exchange 中查找有关 Develop 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