App Designer: Using function outputs as variables within other functions
56 次查看(过去 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
end
methods (Access = private)
function [y] = func(app)
A = app.EditField.Value;
B = A + 1;
y = app.x.^2 + B;
plot(app.UIAxes,app.x,y)
end
I wish to use the variable 'y' from func(app) for use within the second function, func2(app).
func2(app)
plot(app.UIAxes2,app.x,y)
end
end
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,
Sam
(Using R2017b)
0 个评论
采纳的回答
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:
https://www.mathworks.com/help/matlab/creating_guis/share-data-across-callbacks-in-app-designer.html
For example, create a new property 'y' in your 'properties'.
properties (Access = private)
x = linspace(0,1,10); % Description
y;
end
Then, in func(app), set
app.y = app.x.^2 + B;
Then, in func2(app), you can have
plot(app.UIAxes2,app.x,app.y)
更多回答(1 个)
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 个评论
Walter Roberson
2022-4-13
That example works by having the calling program provide an update method that the called app is responsible for invoking in order to export changes. That is certainly a valid thing to do, but it is not the same as saying that your app has "output".
You asked "how do i send the updated data back to the mlapp that called myapp" and the example you are looking at does not do that: instead it tells the calling app to update itself. Different dynamic.
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 :)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!