Cannot Break A Function Loop With GUI Button

조회 수: 2 (최근 30일)
Beril Sirmacek
Beril Sirmacek 2018년 7월 31일
댓글: Beril Sirmacek 2018년 8월 1일
I have a GUI which has START and STOP buttons with functions below. START button starts a live camera stream. However, I cannot break the loop by pushing to STOP button. How can I stop the camera acquisition? It seems like the STOP button is ignored until the WHILE loop ends. The STOP button is not read during the loop process.
I'd be grateful to hear your suggestions to make the STOP button working.
%%Start button
function pushbutton1_Callback(hObject, eventdata, handles)
global vid;
global stop_pressed;
handles = guidata(hObject);
stop_pressed = get(handles.pushbutton2, 'Value');
disp('START LIVE STREAM button pressed')
% set camera settings:
vid = cameraSetup();
max_frame_no = 10000;
start(vid);
frame_no = 1;
while (frame_no < max_frame_no)
if stop_pressed == 1
break;
end
img = getsnapshot(vid);
axes(handles.axes1);
imshow(img);
frame_no = frame_no + 1;
end
stop(vid);
%%STOP button
function pushbutton2_Callback(hObject, eventdata, handles)
global stop_pressed;
disp('STOP LIVE STREAM button pressed')
guidata(hObject,handles);
stop_pressed = get(handles.pushbutton2, 'Value');
  댓글 수: 1
Dennis
Dennis 2018년 7월 31일
Check stop_pressed in your pushbutton2_Callback. If you are using a pushbutton instead of a togglebutton your pushbutton Value will be 0.

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

답변 (2개)

Adam
Adam 2018년 7월 31일
The attached files give an example of a start-stop GUI which works relatively simply. Don't use global variables to pass data around though - they may or may not be contributing to the problem here, but they bring so many problems they are really not worth it and are pretty much never a good solution to any problem.
  댓글 수: 1
Beril Sirmacek
Beril Sirmacek 2018년 7월 31일
Thank you for sharing the example. It indeed works with this demo, however it doesn't stop the camera stream when I put imshow function in the while loop.

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


Beril Sirmacek
Beril Sirmacek 2018년 7월 31일
Looks like I fixed it.
All I do is that I have added a "drawnow;" comment after imshow within the while loop.
I am still not sure what "drawnow" is doing differently, but it somehow makes the loop breakable.
Thanks for your attention.
  댓글 수: 2
Adam
Adam 2018년 8월 1일
As stated in
doc drawnow
drawnow updates figures and processes any pending callbacks so it is like having a pause instruction in that it provides an interrupt opportunity in code (as well as updating the UI).
Beril Sirmacek
Beril Sirmacek 2018년 8월 1일
True. Thank you!

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

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by