フィルターのクリア

how to stop while loop by right mouse button click

9 ビュー (過去 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
  1 件のコメント
Sergey Lopatnikov
Sergey Lopatnikov 2017 年 2 月 19 日
It definitely does not work.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by