About updating value of handles whithin loop

1 view (last 30 days)
function pushbutton4_Callback(hObject, eventdata, handles)
handles.st=1
guidata(hObject, handles);
this is the code to stop video with push button, to start the video:
function pushbutton2_Callback(hObject, eventdata, handles)
info=imaqhwinfo('winvideo')
vid=videoinput('winvideo',1)
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 50;
start(vid)
sto=0;
while(sto==0)
if(vid.FramesAcquired>=10)
break;
end
data = getsnapshot(vid);
axes(handles.axes1)
imshow(data)
guidata(hObject, handles);
drawnow;
pause(2);
sto=handles.st
end
the problem is when i press stop button and update st to 1 it take sto as well as st 0 always
  1 Comment
Stephen23
Stephen23 on 26 Oct 2015
@Devendra Singh: I formatted your code correctly for you, but in future you can do it yourself by selecting the code text and then clicking the {} Code button. It also helps if you do not have empty lines as every alternate line of the code.

Sign in to comment.

Answers (1)

Dennie
Dennie on 26 Oct 2015
Matlab GUI's use callbacks to handle the button interrupts. However, the callbacks cannot interrupt each other. Matlab use a sequential method for this, meaning that when you press two buttons in the GUI it first finishes the first callback and afterwards the second. This is the reason that your value is not being updated during the while loop.
A trick that you could try to break the loop is using a toggle stop button and read out that value in the loop
while(sto==0)
if(vid.FramesAcquired>=10)
break;
end
data = getsnapshot(vid);
axes(handles.axes1)
imshow(data)
guidata(hObject, handles);
drawnow;
pause(2);
sto=get(handles.stopbutton,'Value');
end
This will force matlab to read out the current value of the button and since you use a toggle button, you don't have to worry about timing.
  1 Comment
Devendra Singh
Devendra Singh on 27 Oct 2015
Dennie Sir,could you please suggest what to write in toggle button callbacks .

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!