how to stop while loop by right mouse button click

11 visualizaciones (últimos 30 días)
Sergey Lopatnikov
Sergey Lopatnikov el 18 de Feb. de 2017
Editada: Stephen23 el 19 de Feb. de 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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Feb. de 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')

Más respuestas (1)

Chad Greene
Chad Greene el 18 de Feb. de 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

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by