How do I interrupt a while loop with recursive, interrupting callback of checkbox in GUI
Mostra commenti meno recenti
Hi all,
I have a GUI designed to record mouseclicks on an axis. The recording should start when I push a checkbox (Value==1 is true), and end when I push the checkbox again (Value==0 is true)
My code follows, and currently does the job, but it is unsatisfying. My current workaround centers around the line:
pause(0.3) %Allow interrupting callbacks to execute
However, this line is very finicky. Changing it to "pause(0.05)" results in the while loop finishing before the interrupting callback, and the code gets stuck on waitforbuttonpress even after completion of the interrupting callback. I've tried "drawnow" as well, with similar unintended results (code prematurely finishes while loop and gets "stuck" on waitforbuttonpress on the next unintended loop).
Am I missing something? Trying to write good code that doesn't involve an artificial pause for 0.3 seconds for interrupting callbacks to "catch up."
function record_cb_Callback(hObject, eventdata, handles)
if hObject.Value==1 %Executed on first checkbox click.
handles.stoprecord=0;
guidata(hObject,handles);
while handles.stoprecord ==0;
waitforbuttonpress %Could be in axes1 or on current checkbox
pause(0.3) %Allow interrupting callbacks to execute
handles = guidata(hObject);
%This is what I'm trying to record, but is not pertinent to
%question
if handles.newaxclick
handles.deformFuncX = [handles.deformFuncX, handles.axClickX];
handles.deformFuncY = [handles.deformFuncY, handles.axClickY];
handles.newaxclick=0;
end
guidata(hObject,handles);
end
elseif hObject.Value==0 %Executed on recursive interrupting callback
handles.stoprecord = 1; %This needs to execute before the whileloop in the first callback completes
guidata(hObject,handles);
end
%For completeness, here is a related callback used in recording my axis
%clicks
function axes1_ButtonDownFcn(hObject, eventdata, handles)
handles.axClickX = hObject.CurrentPoint(1,1);
handles.axClickY = hObject.CurrentPoint(1,2);
handles.newaxclick = 1; %Tell other callbacks to react to a click in the axis. Other callbacks must reset back to 0.
guidata(hObject,handles);
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Interactive Control and Callbacks in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!