[英]How to get coordinates of the clicked point when data cursor mode is on in matlab?
我正在嘗試在我不熟悉的 Matlab 中設計和編程 GUI。
基本上,我有兩個組件,即“軸”和“列表框”。 坐標區中有一個 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沒有被觸發。
我將從為數據游標創建自己的更新功能開始
% in your main .m file
hdt = datacursormode;
set(hdt,'UpdateFcn',{@labeldtips,hdt});
然后你可以像這樣獲得該函數中的位置:
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 [xy] to the list box
嘗試對代碼中剩余的部分進行此操作。 請記住在您的情況下將“listbox1”編輯為用於列表框的標簽 -
contents = cellstr(get(handles.listbox1,'String'));
str1 = [ '[' num2str(x) ' ' num2str(y) ']' ];
contents(size(contents,1)+1,1) = {str1};
set(handles.listbox1,'String',contents);
讓我們知道它是否有效!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.