While loop not breaking even though condition is met.
이전 댓글 표시
I have an issue where my while loop continues to run even though my if statement, containing a break, inside the loop is met. I have tested this by displaying strings. It does print 'moving stage' when a push the button as well as 'stop stage' when i push it again. The flags I have defined changes between 1 or 0 depending on a button i push in a GUI. How can I terminate my loop?
function Standa_cmd(Stage_functions_settings)
while 1
pause(1)
disp('moving stage')
flag = Stage_functions_settings.move_x; % This struct field changes value between 1 or 0 depending on the button push in the gui
if(flag == 0)
disp('stop stage')
break
end
end
end
댓글 수: 3
VBBV
2022년 11월 11일
if strcmp(flag,'0')
Check with strcmp. May be button pressed is returning char
Jiri Hajek
2022년 11월 11일
Hi, what you have described does not prove that the break command does not work, which you seem to suggest. Please put a breakpoint inside the if block and then use the F10 key to see what happens... This way, if you are able to get the process to the breakpoint, you will verify the correct functioning of the break command.
Christoffer Sørensen
2022년 11월 11일
채택된 답변
추가 답변 (1개)
Image Analyst
2022년 11월 11일
0 개 추천
I think the pushbutton does not set the flag. I've seen this in GUIDE and this is why I use checkboxes instead of a push button. Then simply check the value of the checkbox inside your while loop.
Nonetheless it is possible to use a push button. See attached little demo.
댓글 수: 2
Christoffer Sørensen
2022년 11월 11일
Bruno Luong
2022년 11월 11일
"I think the pushbutton does not set the flag" Toggle button yes.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!