Hi, everyone,
I'm doing an app in the App designer and I got the following problem. I have some components and I need to do some process (always the same) when any of the components value changes. I would like to know if there is a way to put everything together in the same callback. This is something that will help me to avoid repetition of lines in the code.
I'm doing something like
ValueChanged callback in component one
Calculation ComponentOne.Value+ComponentTwo.Value
ValueChanged callback in component two
Calculation ComponentOne.Value+ComponentTwo.Value
What I would like to do is
ValueChanged callback in component one or two
Calculation ComponentOne.Value+ComponentTwo.Value
Thanks in advance

댓글 수: 1

Mario Malic
Mario Malic 2020년 10월 7일
Write it in a helper function, and in the callback, call the function

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

 채택된 답변

Mohammad Sami
Mohammad Sami 2020년 10월 8일

0 개 추천

You can assign the same function as callback to multiple ui components.
Inside you function you will need to be able to access the properties under app directly.
function CommonValueChanged(app, event)
app.ComponentOne.Value + app.ComponentTwo.Value
end

댓글 수: 5

Hi Mohammad, I created a Third component to prove your solution but it doesn't changes the value in ComponentThree.
methods (Access = private)
function CommonValueChanged(app,event)
app.ComponentThree.Value=app.ComponentOne.Value + app.ComponentTwo.Value;
end
end
I don't know what is missing or maybe I misunderstood your solution.
Mohammad Sami
Mohammad Sami 2020년 10월 8일
What is the type of component three ? Also just to verify the callback for component one and two are set to CommonValueChanged ?
Component Three it's a Numeric editfield
But are you saying this?
methods (Access = private)
function CommonValueChanged(app,event)
app.ComponentThree.Value=app.ComponentOne.Value + app.ComponentTwo.Value;
end
end
function ComponentTwoValueChanged(app, event)
CommonValueChanged(app,event);
end
function ComponentTwoValueChanged(app, event)
CommonValueChanged(app,event);
end
That works but I don't want that because there are two callbacks and I just want one common callback. Imagine that you want to do the same for say 15 components , in that case it would be neccesary to write 15 callbacks and I think it's to much code.
Mohammad Sami
Mohammad Sami 2020년 10월 9일
Thats not what I meant.
You can right click on each component on the right hand pane.
For the first component, click add value changed fcn to create a callback. For the second component, Click Select Exisiting Callback and set it to same callback as one.
I have also attached a small demo.
Juan Ruiz Osorio
Juan Ruiz Osorio 2020년 10월 9일
Perfect!. That's exactly what I was looking for,
Thank you,Mohammad.

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

추가 답변 (0개)

카테고리

도움말 센터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!

Translated by