MATLAB Answers

Pass Variable to Calling App

5 views (last 30 days)
I have searched around for the best practice to pass an app variable back to the calling app. For example, app1 is the main app and calls app2. I can easily pass variables into app2 via h = app2(a, b) when I run it from app1. However, I would like to pass back a and b to the calling app when app2 is closed. I have been using setappdata and getappdata, but don't believe this is technically the right approach.
app1 pseudocode:
function ButtonPress(app)
a = 5;
b = 10;
h = app2(a,b); % app2 modifies a and b
uiwait(h.UIFigure)
% at this point when app2 is closed, i want a and b returned to app1
end

  0 Comments

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 28 Apr 2020
Please refer to the following documentation on how to pass data between appdesigner apps.
https://www.mathworks.com/help/matlab/creating_guis/creating-multiwindow-apps-in-app-designer.html

  3 Comments

Chris Nemecek
Chris Nemecek on 29 Apr 2020
I see that the suggested solution is to use a public function defined in app1 and called in app2 to set some private properties of app1.
Is this equivalent to the above: defining a public property in app1, passing in an instance of app1 upon startup of app2, and setting the public property of app1 within app2?
Is one approach better than the other?
Mohammad Sami
Mohammad Sami on 30 Apr 2020
The second approach will work as well.
In my opinion using a function to update the properties may be preferable in certain cases.
Example if you want the change to be reflected in the UI in any way. The function can execute additional code to update the UI in addition to setting the properties.
A function can also do validation checks e.t.c.
Chris Nemecek
Chris Nemecek on 30 Apr 2020
Those are good points. Thank you.

Sign in to comment.

More Answers (0)

Products


Release

R2018b