MATLAB: How to control the while loop iterations using a pushbutton and selecting data using datacursormode upon each iteration

datacursormodepushbuttonuicontrolwhile loop

**How can I control the while loop iterations with a pushbutton (to make it stop the loop) where in each loop iteration I am selecting data in a plot using datacursormode?
Here is my code that I have so far just as an example. It seems to work for the most part except it doesn't save the data I select on the 2nd iteration, which I can't correct.**
%% Here is the code below
fig = figure z = peaks; plot(z(:,30:35)) next=1 ButtonHandle = uicontrol('style','push', 'callback','next=0','userdata',0) ; newStat=[]; numberofselections=1
while next==1
dcm_obj = datacursormode(fig);
set(dcm_obj,'DisplayStyle','datatip',...
'SnapToDataVertex','off','Enable','on')
disp('Click line to display a data tip, then press Return.')
% % Wait while the user does this.
pause
c_info = getCursorInfo(dcm_obj);
if numberofselections==1, newStat = [c_info.Position(1), c_info.Position(2)].'; else newStat = [newStat [c_info.Position(1), c_info.Position(2)].']; end
%Make selected line wider
set(c_info.Target,'LineWidth',2)
numberofselections = numberofselections + 1 ;
pause
disp('either click button to stop or just hit enter')
end

Best Answer

I haven't been able to do it with a pushbutton. I've only been able to do it with a checkbox. You can check the checkbox state in the loop. But having a push button set some flag and then checking it in the loop does not seem to work, at least the way I tried it.