What is the the good practice for exchanging data in App Designer?

Let's start with a simple example:
We're having a button callback that calculates the numeric field value from two other numeric fields in the application.
function ButtonPushed(app, event)
app.VariableAField1.Value = app.VariableBField2.Value*app.VariableCField3.Value
end
Then we call a helper function
function helpfun(app.VariableAField1.Value, app.VariableBField2.Value)
x = app.VariableBField2.Value
y = app.VariableAField1.Value;
plot(x,y)
end
If we had a bit more variables to pass in the helpfun, then input arguments in the helpfun would be quite long, and maybe unreadable.
The way that I did is to work with public properties, which I defined in the helper function and called within startupFcn. But, public properties operate in a similar way in Application (or am I wrong?) as the global variables operate in scripts, so that is what I am worried about.
Solution with properties:
function helpfun(app)
% app.x and app.y have default values
% when the user updates respective field, callback will update x and y property values
plot(app.x,app.y)
end
I would appreciate a light discussion on this question.
If this question should be on the pile of questions that are related to the usage of global variables, I welcome its confirmation and locking (with the suggestion of the common practice).
Thank you in advance.

 채택된 답변

Mohammad Sami
Mohammad Sami 2020년 9월 7일
편집: Mohammad Sami 2020년 9월 7일
No public / private properties are not the same as global variables.
They are only available in the non static functions defined in the app or in classes which inherit from your app. Public properties can also be access in external functions, if app is passed in as an input arguments.
Inisde the function you always have to access them as app.property.
function internalfunction(app)
app.property1;
app.privateproperty1; % only accessible in functions defined in app
end

댓글 수: 1

I waited a bit to see if someone else will reply as well.
Thank you very much for clarifying my question.
Hope you have a great day!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2020년 9월 6일

댓글:

2020년 9월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by