how to stop while loop by right mouse button click

조회 수: 17 (최근 30일)
Sergey Lopatnikov
Sergey Lopatnikov 2017년 2월 18일
편집: Stephen23 2017년 2월 19일
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.

채택된 답변

Walter Roberson
Walter Roberson 2017년 2월 19일
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')
  댓글 수: 1
Sergey Lopatnikov
Sergey Lopatnikov 2017년 2월 19일
편집: Stephen23 2017년 2월 19일
Thank you, it works perfectly.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chad Greene
Chad Greene 2017년 2월 18일
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

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by