繁体   English   中英

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

[英]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.

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