2012-11-23 653 views
3

我想單擊一個Matlab圖,找出點擊位置的x和y位置。如何在Matlab圖形軸上獲得鼠標點擊位置?

在圖中,我認爲有一種方法可以點擊線上的點並獲取它的x和y座標。如果沒有繪製圖表,我該怎麼做?

+0

當你說「如果沒有繪製圖表,我該怎麼做」,這並不明確。但是你可能想要使用ginput()。例如。看到這裏:[越來越像素座標有效在MATLAB](http://stackoverflow.com/questions/6541444/getting-pixel-coordinates-efficiently-in-matlab) – Justin

+0

我想點擊空的某處情節(我認爲matlab稱之爲座標軸而不是數字,因爲圖(1)不會生成圖形實際顯示的白色畫布)。我想預定義軸的x和y座標的最大和最小限制,當我點擊座標軸上的空白區域時,我希望能夠將點的x和y座標存儲在變量中 – user13267

+1

您可以使用回調。 – bonCodigo

回答

5

一旦你創建了數字嘗試

​​3210

等你拿上圖點擊一次(這就是爲什麼論點之一),你會得到的座標返回的功能ginput()返回。

6

這裏是如何做到這一點最優雅:

function test 

    % create test figure 
    f = figure(1); 

    % set function to call on mouse click 
    set(f, 'WindowButtonDownFcn', @clicker); 


end 

% function called on mouse click in the figure 
function clicker(h,~) 


    get(h, 'selectiontype') 
    % 'normal' for left moue button 
    % 'alt' for right mouse button 
    % 'extend' for middle mouse button 
    % 'open' on double click 

    get(h, 'currentpoint') 
    % Current mouse location, in pixels from the lower left. 
    % When the units of the figure are 'normalized', the 
    % coordinates will be [0 0] inb lower left, and [1 1] in 
    % the upper right. 

end 
1

也許這也將工作:

function [loc] = get_image_point (I) 
    figure('name','Doubleclick to set location');imshow(I); 
    [c r] = getpts(1); 
    loc = int32([c r]); 
    if size(loc,1)>1 
     loc = [loc(1,1) loc(1,2)]; 
    end 
    close all; 
end 

斯蒂芬

0

注: - getpts() - 是一個 「圖像處理工具箱」功能。 - ginput() - 等待您的鼠標單擊,暫停執行直到您點擊,並且僅在被調用時才起作用。

get(h, 'currentpoint')只要你的程序正在運行就可以隨時工作。

相關問題