How to assign a callback to a gui element created at runtime

조회 수: 3 (최근 30일)
Ilario Triscari
Ilario Triscari 2019년 8월 27일
댓글: Ilario Triscari 2019년 8월 28일
Hi,
In AppDesigner I created an app that creates a slider at runtime:
Tt_end_slider = uislider();
Tt_end_slider.Position = app.emptySlider_2.Position;
Tt_end_slider.Tag='t_end';
Tt_end_slider.UserData=i;
Tt_end_slider.ValueChangedFcn = @(source, event) app.osc_prev_slider_ValueChanged ;
The Callback is defined as follows
% Callback function
function osc_prev_slider_ValueChanged(app, event)
value = event.Source.Value;
[..]
end
Using the slider, I get this error:
Not enough input arguments.
Error in tutorialApp/osc_prev_slider_ValueChanged (line 575)
value = event.Source.Value;
Error in tutorialApp>@(source,event)app.osc_prev_slider_ValueChanged (line 325)
Tt_start_slider(i).ValueChangedFcn = @(source,event)app.osc_prev_slider_ValueChanged ;
I couldn't find how to correctly pass the requested argument to the callback ( I tried with @(source,event) looking at the callbacks assigned using the graphic interface)

채택된 답변

Cris LaPierre
Cris LaPierre 2019년 8월 27일
편집: Cris LaPierre 2019년 8월 27일
You're close. I see two errors.
1. "Not enough input arguments" is because you are not passing 2 inputs to your callback function. App Designer automatically passes app, but you need to pass in something for event. Update your ValueChangedFcn to this
Tt_end_slider.ValueChangedFcn = @(source, event) app.osc_prev_slider_ValueChanged(Tt_end_slider);
2. Inside your callback function, you are using incorrect dot notation to get the value of the slider. Remove ".Source".
value = event.Value;
  댓글 수: 1
Ilario Triscari
Ilario Triscari 2019년 8월 28일
Thank you, now it works. However I did not understand the difference between arguments in the first parenthesis and arguments in the second one.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by