how to stop while loop by right mouse button click

11 vues (au cours des 30 derniers jours)
Sergey Lopatnikov
Sergey Lopatnikov le 18 Fév 2017
Modifié(e) : Stephen23 le 19 Fév 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.

Réponse acceptée

Walter Roberson
Walter Roberson le 19 Fév 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')
  1 commentaire
Sergey Lopatnikov
Sergey Lopatnikov le 19 Fév 2017
Modifié(e) : Stephen23 le 19 Fév 2017
Thank you, it works perfectly.

Connectez-vous pour commenter.

Plus de réponses (1)

Chad Greene
Chad Greene le 18 Fév 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

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by