[英]Get coordinates of clicks in figure AND specific mouse button clicked for each point clicked
Matlab的后續問題:獲取圖形中的點擊坐標,但保持按鈕回調 。
很棒的代碼。
提供的功能僅輸出最后一次點擊的“按鈕”。 有沒有一種方法可以修改代碼,以輸出針對每個選定點單擊的特定鼠標按鈕?
另外,有沒有辦法提供一個第三種輸入,該輸入可用於指定要選擇的點數未知,並繼續選擇點,直到輸入諸如“ c”或“ return”之類的內容為止。鍵盤?
關於您的第二個要求,當用戶按下一個鍵時,控件將從圖形窗口移至命令窗口,除非您使用的是waitforbuttonpress
,這會不必要地使事情復雜化。 因此,可以建議使用雙擊來表示輸入坐標結束的替代解決方案。 這是在下面顯示的鏈接的stackoverflow代碼的修改版本中完成的。
碼
function varargout = ginput_ax_mod2(ha,n)
if nargin<2
n=1;
end
k = 0;
button = 0;
%%// Tolerance so that in the inifnity case, this could act as
%%// the thresholding distance below which the
%%// input extracting operation must be terminated
TOL = 0.01;
%%// Placeholders for X-Y and button type could be stored
button1 = [];
xy = [];
hf = get(ha,'parent');
figure(hf);
set(hf,'WindowButtonMotionFcn',@changepointer)
set(ha,'ButtonDownFcn',@getpoints)
hp = get(ha,'children');
ht = get(hp,'hittest');
set(hp,'hittest','off')
axlim = get(ha,'Position');
fglim = get(hf,'Position');
x1 = axlim(1)*fglim(3) + fglim(1);
x2 = (axlim(1)+axlim(3))*fglim(3) + fglim(1);
y1 = axlim(2)*fglim(4) + fglim(2);
y2 = (axlim(2)+axlim(4))*fglim(4) + fglim(2);
waitfor(hf,'WindowButtonMotionFcn',[])
if iscell(ht)
for jj=1:length(ht)
set(hp(jj),'hittest',ht{jj})
end
else
set(hp,'hittest',ht)
end
selType = get(hf,'SelectionType');
% Mouse-Button recognition...
if(strcmp(button, 'normal'))
button = 1; % left
elseif(strcmp(button, 'extend'))
button = 2; % right
elseif(strcmp(button, 'alt'))
button = 3; % middle
else
button = 4; % double click any mousebutton
end
if nargout==3
varargout{1} = xy(:,1);
varargout{2} = xy(:,2);
varargout{3} = button1(:,1);
elseif nargout==2
varargout{1} = xy(:,1);
varargout{2} = xy(:,2);
else
varargout{1} = xy;
end
function changepointer(~,~)
pntr = get(0,'PointerLocation');
if pntr(1)>x1 && pntr(1)<x2 && pntr(2)>y1 && pntr(2)<y2
set(hf,'Pointer','crosshair')
else
set(hf,'Pointer','arrow')
end
end
function getpoints(src,evnt)
cp = get(src,'CurrentPoint');
button = get(hf, 'SelectionType');
k = k+1;
if k==1
xy = [xy ;cp(1,1:2)];
button1 = [button1; {button}];
end
if k>=2
if pdist2(cp(1,1:2),xy(k-1,:))<TOL && isinf(n)
k = n;
else
xy = [xy ;cp(1,1:2)];
button1 = [button1; {button}];
end
end
if k==n
set(hf,'Pointer','arrow')
set(hf,'WindowButtonMotionFcn',[])
set(ha,'ButtonDownFcn',[])
return;
end
end
end
樣品運行
運行1:無限點情況
[x_coords,y_coords,button_types] = ginput_ax_mod2(gca, Inf)
運行2:10分情況
[x_coords,y_coords,button_types] = ginput_ax_mod2(gca, 10)
示例輸出具有無限大小寫,但在第三次單擊后雙擊-
x_coords =
2.0472
4.3076
5.9873
y_coords =
24.4152
25.2924
26.7544
button_types =
'normal'
'alt'
'normal'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.