简体   繁体   English

在matlab中打开数据光标模式时如何获取点击点的坐标?

[英]How to get coordinates of the clicked point when data cursor mode is on in matlab?

I'm trying to design and program a GUI in Matlab, with which I'm not familiar.我正在尝试在我不熟悉的 Matlab 中设计和编程 GUI。

Basically, I have two components, which are "axes" and "list box".基本上,我有两个组件,即“轴”和“列表框”。 There is an RGB image in the axes.坐标区中有一个 RGB 图像。 I'm planning to add the selected point to the list box.我打算将选定的点添加到列表框中。

The following code works just fine, but I would like to make it work when the data cursor is on.下面的代码工作得很好,但我想让它在数据光标打开时工作。

How can I make it work when the data cursor is on?数据游标打开时如何使其工作?

% 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

Edit 1: The problem is that the function imageButtonDownFcn is not triggered when data cursor is on.编辑1:问题是当数据光标打开时函数imageButtonDownFcn没有被触发。

I would start by making a own update funktion for the data cursors我将从为数据游标创建自己的更新功能开始

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

Then you can get the position in that function like this:然后你可以像这样获得该函数中的位置:

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

then you have the position in pos and you can add %Some code to add the [xy] to the list box again然后你在pos中有位置,你可以添加%Some code to add the [xy] to the list box

Try this for the part that is remaining in your code.尝试对代码中剩余的部分进行此操作。 Remember to edit "listbox1" to the tag used for listbox in your case -请记住在您的情况下将“listbox1”编辑为用于列表框的标签 -

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

Let us know if it works!让我们知道它是否有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM