MATLAB Answers

How can I control the while loop iterations using a pushbutton and selecting data using datacursormode upon each iteration?

5 views (last 30 days)
Eric
Eric on 4 Aug 2014
Commented: Eric on 4 Aug 2014
**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

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 4 Aug 2014
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.

  3 Comments

Eric
Eric on 4 Aug 2014
Could you share your code with the checkbox state? That should work for me, I just need to be able to tell it to stop the while loop after selecting some arbitrary number of points on a plot. Thanks for your fast response
Image Analyst
Image Analyst on 4 Aug 2014
Before the loop
set(handles.chkFinishNow, 'Visible', 'on');
set(handles.chkFinishNow, 'Value', 'off');
Then start your loop and check at the bottom
for or while loop....
% Check if the "Finish Now" checkbox is checked. Bail out if it is.
chkFinishNow = get(handles.chkFinishNow, 'Value');
if chkFinishNow
break;
end
end % of the loop over images.
After the loop
% Make the "Finish Now" checkbox invisible and unchecked.
set(handles.chkFinishNow, 'Value', 0, 'Visible', 'off');
Eric
Eric on 4 Aug 2014
Your suggestion worked along with using the "Menu" function where when you set one option for the menu as "Select Additional Data Points" and the other "Quit" (I borrowed this from one of your other answers, so you still helped me out either way). Thanks! Here is the same thing but with the Menu function:
fig = figure z = peaks; plot(z(:,30:35))
newStat=[]; numberofselections=0
button = 1
while button == 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.
numberofselections = numberofselections + 1 ;
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)
drawnow
pause
disp('either click button to stop or just hit enter')
button = menu('What do you want to do?', 'Select Additional Data Points', 'Quit');
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!