App Designer: Using function outputs as variables within other functions

187 ビュー (過去 30 日間)
I would like to store a variable as an output from a function in App Designer for use with other functions. Please see the simple example below.
properties (Access = private)
x = linspace(0,1,10); % Description
methods (Access = private)
function [y] = func(app)
A = app.EditField.Value;
B = A + 1;
y = app.x.^2 + B;
I wish to use the variable 'y' from func(app) for use within the second function, func2(app).
Any help would be appreciated, the example is trivial as both plots plot the same graph. I am just unaware of the syntax required as the App Designer environment has slight modifications from that of the usual MATLAB workspace.
Any help would be very appreciated,
(Using R2017b)


Chris Perkins
Chris Perkins 2017 年 12 月 15 日
編集済み: Chris Perkins 2017 年 12 月 15 日
Hi Sam,
The best way of storing data in one function in an AppDesigner app for use in another function is to create a new 'property', and set the property's value instead of creating a new local variable. Then, in your other functions, you have access to the app's properties and can use any property as needed.
The following documentation page describes this process in more detail, with examples:
For example, create a new property 'y' in your 'properties'.
properties (Access = private)
x = linspace(0,1,10); % Description
Then, in func(app), set
app.y = app.x.^2 + B;
Then, in func2(app), you can have
  1 件のコメント
Samuel Thompson
Samuel Thompson 2017 年 12 月 18 日
Ah that's perfect, thanks for your help!


その他の回答 (1 件)

Mark Eigenraam
Mark Eigenraam 2022 年 4 月 11 日
As a follow on, I had a similar problem. if I run the following a = myapp (pass_in_data), after the user has made changes to pass_in_data, how do i send the updated data back to the mlapp that called myapp. Thanks
  6 件のコメント
Mark Eigenraam
Mark Eigenraam 2022 年 4 月 13 日
Yes you are correct. I have written code (based on the Mathworks example) in the calling app now that updates its data, without closing the child app. I could have used better phrasing :)



Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by