2014-03-03 33 views
3

我試圖在Matlab中設計和編程一個GUI,但我並不熟悉它。如何在matlab中的數據光標模式打開時獲取點擊點的座標?

基本上,我有兩個組件,它們是「軸」和「列表框」。軸上有一個RGB圖像。我打算將選定的點添加到列表框中。

下面的代碼工作得很好,但我想讓它在數據光標打開時工作。

如何在數據光標打開時使其工作?

% 100x100x3 RGB image 
RgbImage = randi(100, 100, 100, 3); 

% Draw the image 
axesHandle = axes(); 
imageHande = imagesc(RgbImage); 
axis image; 

% ButtonDownFc 
set(imageHandle, 'ButtonDownFcn', @imageButtonDownFcn); 
function imageButtonDownFcn(hObject, eventdata) 
    p = get(gca, 'CurrentPoint'); 
    x = floor(p(1)); 
    y = floor(p(2)); 

    % Some code to add the [x y] to the list box 
end 

編輯1: 的問題是,當數據光標在功能imageButtonDownFcn不會被觸發。

+1

從這裏獲取靈感 - http://www.mathworks.in/matlabcentral/fileexchange/38997-ginput-on-gui-keeping-toolbar-features-on-use-of-custom-pointers – Divakar

回答

0

我會爲得到這一數據的遊標

% in your main .m file  
hdt = datacursormode; 
set(hdt,'UpdateFcn',{@labeldtips,hdt}); 

一個自己的更新的Funktion啓動然後你就可以得到這樣在功能位置:

function output_txt = labeldtips(obj,event_obj,hdt) 
% Display an observation's Y-data and label for a data tip 
% obj   Currently not used (empty) 
% event_obj Handle to event object 

dcs=hdt.DataCursors; 
pos = get(dcs(1),'Position'); %Position of 1st cursor 

output_txt{1} = ['X: ', num2str(pos(1))]; 
output_txt{2} = ['Y: ', num2str(pos(2))]; %this is the text next to the cursor 
end 

,那麼你必須在pos位置並且您可以再次添加%Some code to add the [x y] to the list box

0

對代碼中剩餘的部分嘗試此操作。記得編輯「listbox1」到您的案例中用於列表框的標籤 -

contents = cellstr(get(handles.listbox1,'String')); 
str1 = [ '[' num2str(x) ' ' num2str(y) ']' ]; 
contents(size(contents,1)+1,1) = {str1}; 
set(handles.listbox1,'String',contents); 

讓我們知道它是否有效!

+0

實際上,問題是不會觸發函數imageButtonDownFcn。 –

+0

您是否嘗試過GINPUT - http://www.mathworks.in/help/matlab/ref/ginput.html? – Divakar