Search code examples
matlabmatlab-app-designer

How to use properties to share data between two apps in MATLAB App Designer?


I have created two apps (named "firstapp", "secondapp" respectively) by using MATLAB App Designer and I want them to share data and variables which will be entered in cells.

At first, the first app opens the second by pushing a button. This part is clear. Besides this, I'm trying to give some input variables to the second app and send a basic calculation of these input variables to my first app. The problem is when I push the button in the first app in order to open the second app, several errors are occurring.

While doing this, I've added a public property, including the input arguments (a, b, c) and the result of them, and send the result to the first app by pushing a button.

The codes in the second app which are expected to keep input arguments and send the result variable to the first app are here:

properties (Access = public)
    a = secondapp.aEditField.Value;
    b = secondapp.cEditField.Value;
    c = secondapp.cEditField.Value;
    result = (a + b)^c;
end

The button function:

function CalculateButtonPushed(app, event)
    firstapp.result2 = secondapp.result;
end

I only want to send the result to the first app but I encounter several error messages:

1) "Invalid default value for property 'a' in class 'secondapp' "

2) "The property 'aEditField' in class 'secondapp' must be accessed from a class instance because it is not a Constant property."


Solution

  • You need to add an argument to your second app. It can be done in the code view, by pressing the "App Input Arguments", a dialog will appear. Add "firstapp" as an argument.

    In your first app, when opening the second app by pressing a pushbutton. You have to call the second app this way.

    secondapp(firstapp);
    

    You will be able to access to all the public properties of your first app from your second app.

    Please refer to the documentation for more details. https://www.mathworks.com/help/matlab/creating_guis/app-designer-startup-function.html