Passing Data between Apps in app designer - Table Data in app1 plotted in app2

Hello,
I am trying to pass data from one applicationto another in matlab. Specifically, I am trying to pass the data that the user has defined in UITable data in the first app to a figure in the second app. I have gone through the documentation and the examples MATLAB provides. I have read through others solutions, and I truly do not understand the logic or the execution of this process.
Only column 2 in the UITable is relevant for the plot in the other application. Here are pictures of my setup. I blocked off the the other buttons and text to make it more simple. App 1 is the one with the table. App 2 is the one with the graph. When I press Visualize Structure in app1, app2 will be opened in order to plot the data from column 2.
Could somone please help me with this? I can provide more details if necessary.
Thanks!

2 Commenti

Hi, thank you for the information.
It worked for me as well.
I have another question: How can I close the window of the first app when the second one opens?
> How can I close the window of the first app when the second one opens?
Use the close command and specify the app's figure handle.
close(app.UIFigure)
You could also use delete but this will not invoke the figure's closing function.
delete(app)

Accedi per commentare.

 Risposta accettata

Attached are two very simple apps to demonstrate multiwindow apps in AppDesigner.
  1. After downloading both files, add the folder to your Matlab path and open demo_MainApp.
  2. From the demo_MainApp, press the button to open the second app.
4 Steps to exchange app handles between multiwindow apps
Open each attached app in AppDesigner to see these 4 steps taken so that both apps have access to both app handles.
1. In demo_MainApp, "app2" was declared as a private variable. You should use a better name, though.
properties (Access = private)
app2 = []; % handle to app2, evoked within this app.
end
2. In demo_MainApp, demo_SecondaryApp is evoked from a callback function. The main app handle is passed to the secondary app.
% Button pushed function: Call2ndappButton
function Call2ndappButtonPushed(app, event)
% Call app2
app.app2 = demo_SecondaryApp(app);
end
3. In demo_SecondaryApp, "app1" was declared as a private variable. You should use a better name, though.
properties (Access = private)
app1 = []; % handle to app1 which evoked this app
end
4. In demo_SecondaryApp, a second input was added to the startup function which recieves the main app handle. The main app handle is added as a property of the secondary app so all internal functions have access to it.
% Code that executes after component creation
function startupFcn(app, app1)
app.app1 = app1;
end
Note that the demo_MainApp handle is input #2 in the startup function of demo_SecondaryApp in step 4 but it appears to be input #1 when it's called from demo_MainApp in step 2. That throws some people off. When an app is evoked, the first input is added internally.
More info

5 Commenti

Thanks for the reply. I tried to go throught thes links before and I am still getting stuck on Implementation. Here is some info first. It seems my scenario is slightly reversed/different then what is given in the MATLAB example (Creating Multiwindow Apps in App Designer). My goal is:
  1. User is working within Table App and creates tabular data
  2. If they want, User can "Visualize Structure" and pass column 2 thickness data over to visualization application. This means the visualization/plotting app does not open until this button is hit.
I may be wrong, but based on the example, I would think that the visualization app would be the "mainapp" and the table app would be the "dialogapp" since this is where the plotting is getting the data. Now because of the order of events that I want, the provided example does not quite fit and I am probably incorrect in my setup. So here are my steps following the tutorial:
Send Information to the Table App
(now, I do not actually want to send any data that is used, but I just followed along)
Again, Dialog Box app = Table app and Main app = visualization app.
  1. Startup Function in Table App. I only need the Thickness Data.
% Code that executes after component creation
function startupFcn(app, mainapp, Thickness)
% Passing Data to Visualization App
app.CallingApp = mainapp;
end
I think I may have something wrong here. I am not clear on what the example means by processing the inputs.
2. Pressing the 'Plot' Button in the visualization app.
function PlotButtonPushed(app, event)
% Setting up data grab from table app
% Disable Plot Button until finished
app.PlotButton.Enable = 'off';
% Getting initial values to pass over (this may not be needed)
Thickness = [];
% Call Table App with these input values
app.TableApp = Table2_Visualization(app,Thickness);
end
Since I did not need to pass over any information, I thought I would leave the variable empty to at least initialize it in some way.
3. Private Poperty in Visualization App that creates property for Table App.
properties (Access = private)
TableApp % Table app (dialog app)
ThicknessData % Stored Thickness data
end
Return Information to the Main App
  1. Public Function in the Visualization App.
methods (Access = public)
function PassInformation(app,Thickness)
% Assign to Property
app.ThicknessData = Thickness;
end
end
2. Private Property in the Table App.
properties (Access = private)
CallingApp % Visualization app object
end
3. Call Public Function within Table App to pass information to the function.
function VisualizeStructureButtonPushed(app, event)
% Call Visualization app's Public Function
% Visualization % Open Application First
PassInformation(app.CallingApp,app.UITable.Data(:,2))
end
Originally, I had the Visualize Structure button opening the Visualize.mlapp. I commented this out since the current setup opens up more itterations of themselves, which I do not want. Additionally, I do not want any of the windows to close, so I did not do that step or the following section of the guide.
Could you please clearify where I may be going wrong? I still am struggling to see how things mesh together. Maybe I need to make a separate demo app that is much similar so I can see it more clearly.
3 quick points.
  1. The "main app" is the app that is initially running and evokes the secondary app.
  2. I've updated my answer to include a demo with two very simple apps that share their app handles.
  3. The problem(s) in your comment above isn't clear. I see you mentioned some uncertainty and I see some blocks of code which are helpful but I don't see a description of the problem you're facing. I think my updated answer will show you the general approach which you can apply to your apps.
General quick feedback from the code in your comment above:
1. I don't think the "Thickness" input is needed; you can get that from the mainapp, hopefully.
function startupFcn(app, mainapp, Thickness)
2. This looks wrong. As mentioned in #1 above, you don't need to pass "thickness" if you can access that from the app handles. You only need the first input below (see my answer for reason why). Also, is TableApp declared as an app property?
app.TableApp = Table2_Visualization(app,Thickness);
Thank you very much for taking the time to help. I think I understand the process now. I was getting confused with when to declare public properties and functions as methods to get the main app handle. I went through your examples and your 4 steps and this is definitely the simplest solution to what I was trying to accomplish. Sorry for not be clear in my questions, but I truly didn't know where to ask or start since I did not understand what I was looking at.
I do have some more questions. First, in step 4,when you have app.app1 = app1, I changed it to app.TableApp = app1 after declaring TableApp as a property in the secondary app. My question is, is app1 (on the right side of the equal sign) a general variable that MATLAB recognizes? Like for instance, if an application (or several applications) is open, then the first application is app1 and the second would be app2?
Second, is there going to be other instances off communicating where this approach is not valid (are there any limitations to this)?
>[ in step 4 ] is app1 (on the right side of the equal sign) a general variable that MATLAB recognizes? Like for instance, if an application (or several applications) is open, then the first application is app1 and the second would be app2?
No. You could test that to find the answer. Notice in steps 1 and 3 I suggest you rename the vars app1 and app2. Those were names I choose for the example.
% for example,
function startupFcn(app,tableApp)
> is there going to be other instances off communicating where this approach is not valid (are there any limitations to this)
I'm not sure what that means. In general those are the 4 steps needed to shareany variable between apps.
Thank you so much for this detailed explanation and the commands - it works for me as well now :) Truly appreciate it, Adam!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Develop Apps Using App Designer in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by