how to stop while loop by right mouse button click
11 views (last 30 days)
Show older comments
Sergey Lopatnikov
on 18 Feb 2017
I would like to stop execution of the while loop by pressung right-click button. I wrote small function to pickup point from graph:
function y=graph_scan_ext (Name,Title,Xax,Yax,u,v)
Name=Name;
Tit=Title;
Xax=Xax;
Yax=Yax;
mainfg=figure();
mainax=axes();
htxt=uicontrol('Style','Text');
set(mainfg,'Name',Name,'NumberTitle','off');
x=u;
y=v;
plot(mainax,x,y);
title(Tit);
xlabel(Xax);
ylabel(Yax);
hold;
mark=0;
q=[];
while mark==0;
c=ginput(1);
scatter(c(1),c(2));
q=[q;c];
prompt = 'To continue press Enter; To stop, press any other key:';
str=input(prompt,'s');
if isempty(str)==0
mark=1;
y=q;
end
end
---------------
I works, but every time you need to confirm that you want to continue by pressing Enter or stop by pressing any other key.
My goal is to replace "key" action with simply right click of mouse button to stop while execution and do nothing to continue. I tried to change "while loop" like this:
while mark==0;
set(mainfg,'SelectionType','normal');
c=ginput(1);
scatter(c(1),c(2));
q=[q;c]
set(mainfg,'Selectiontype','alt');
set(mainfg,'WindowButtonDownFcn',@RightButDownFcn)
function click=RightButDownFcn(mainfg,event_data);
break;
end
y=q;
end
And got
Error: File: graph_scan_rc.m Line: 36 Column: 1
Function definition is misplaced or improperly nested.
0 Comments
Accepted Answer
Walter Roberson
on 19 Feb 2017
ax = gca;
fig = ancestor(ax, 'figure');
q = [];
hold(ax, 'on');
while true
c = ginput(1);
sel = get(fig, 'SelectionType');
if strcmpi(sel, 'alt'); break; end
scatter(c(1),c(2));
q=[q;c]
end
hold(ax, 'off')
More Answers (1)
Chad Greene
on 18 Feb 2017
f = figure(1);
click_type=get(f,'SelectionType');
if strcmp(click_type,'normal') %right click
%Do some stuff
elseif strcmp(click_type,'alt') %left click
%Do some other stuff
end
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!