creating apps communicate each other with app designer

60 次查看(过去 30 天)
How to share a value in a field of an app to another? For example i am trying to get the number in a field to the other app by pushing a button. It is said that the properties would help about that but i could not manage to use them properly.

采纳的回答

Adam Danz
Adam Danz 2020-4-27
编辑:Adam Danz 2023-2-12
> How to share a value in a field of an app with another app?
If you have access to the app's handle or the app's figure handle, then you have access to all app components within the app. See also Matlab's documentation on multi-window apps.
Solution 1: store the app handle
If you invoke app #2 from within app #1 you can gain access to both apps by storing and passing the app handles. For example, within the startupFcn or within any callback function of app #1, open app #2 and store its handle.
app2Handle = app2; % where "app2" is the (poor) name of the 2nd app.
For more info & demos on this method
Solution 2: find the handle to the app's 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 handle that belongs to your app.
% get handles to *all* figures
allfigs = findall(0,'Type','figure');
% isolate the app's handle
app2Handle = findall(allfigs, 'Name', 'MyApp'); % MyApp is the App's fig name
Importantly, "MyApp" in the example above is the name of the app's figure (not necessarily the name of the app). It requires a unique name that can be distinguished from any other figure name.
To name your app's figure,
  1. open your app in AppDesigner and go to Design View
  2. Select the figure background or the top-most component in the Component Browser
  3. From the Inspector list of properties, scroll down to "Identifiers"
  4. use the "Name" property to name your app's figure. This name will appear in your new app figure.
Make the app handle available from anywhere within the app.
To make the app #2 handle available from anywhere within app #1, set the variable containing the app handle as a private or public property by following these instructions from the documentation.
  2 个评论
Raph
Raph 2023-2-1
This is incorrect, this finds the figure, not the actual app object.
What you want is the running instance of the figure.
% get handles to *all* figures
allfigs = findall(0,'Type', 'figure');
% isolate the app's handle
app2Handle = findall(allfigs, 'Name', 'MyApp'); % MyApp is the App's fig name*
A = app2Handle(1).RunningAppInstance; % actual app object.
A.a_public_function() ; % will call the public function from that app
Adam Danz
Adam Danz 2023-2-2
@Raph, the best way to get the app handle is to return it when the app is instantiated (option 1 in my answer and also referenced in the documentation links under option 1). You could also pass is from within the app to another app in a callback function.
The question in this thread is about sharing properties in an app, not about calling public functions in the app. By accessing the app's figure handle, you have access to all app components within the figure. Thus, the alternative options in my answer are feasible, functional solutions to the question.
The undocumented solution you mentioned is also viable solution.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by