简体   繁体   中英

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.

Basically, I have two components, which are "axes" and "list box". There is an RGB image in the axes. 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.

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

Try this for the part that is remaining in your code. Remember to edit "listbox1" to the tag used for listbox in your case -

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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