필터 지우기
필터 지우기

How can I close a while loop in a callback function

조회 수: 3 (최근 30일)
jianshen li
jianshen li 2020년 8월 27일
댓글: jianshen li 2020년 8월 27일
function SwitchValueChanged(app, event)
value = app.Switch.Value;
while(value=='On')
pause(0.05);
end
end
This is a callback for Switch in APP Designer. When Switch is switched to On, the while loop is executed, and when Switch is switched to Off, the loop is terminated.
In fact, when switching to On, after the loop starts, switching the Switch state again will not enter the callback function, resulting in the failure to terminate the while loop.

답변 (1개)

Mehmed Saad
Mehmed Saad 2020년 8월 27일
if value is 'off' the while loop condition will give error because on and off are not of same size
so replace it will strcmp
while(strcmp(value,'On'))
2nd is you are writing callback, and everytime you press switch button, it will call function but with new inputs. In your case, when value is on while loop will run forever so this approach is not right
So donot put while loop in callback, put it in you main code like this
%% Not for callback
value = app.Switch.Value;
while(strcmp(value,'On'))
pause(0.05);
value = app.Switch.Value;
end
  댓글 수: 1
jianshen li
jianshen li 2020년 8월 27일
Thank you for your detailed answers.
I want my code to run when the switch is on and stop when it is off. You said to put the code in the main code. What part of the main code is in the app designer? I'm a beginner about APP Designer.

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

카테고리

Help CenterFile 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