find the x and y index of a value in matlab graph function

675 Views Asked by At

I am writing a customized plot function that shows information along with the data point when its clicked. The input to the function is the figure and an array (of the same size as the information) that have the information to be displayed along with the point.

Here is what I have so far:

function  textPlot( fh, text_v )

    dcm=datacursormode(fh);
    datacursormode on
    set(dcm,'updatefcn',{@myfunction,text_v})

function output_txt = myfunction(obj,event_obj,text_v)

    % Display the position of the data cursor
    % obj          Currently not used (empty)
    % event_obj    Handle to event object
    % output_txt   Data cursor text string (string or cell array of strings).

    pos = get(event_obj,'Position');

    disp(text_v(pos(1)))
    output_txt = {['X: ',num2str(pos(1),4)],...
                  ['Y: ',num2str(pos(2),4)]};

    % If there is a Z-coordinate in the position, display it as well
    if length(pos) > 2
        output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
    end

The problem is that the information will be displayed correctly only if there a one dimensional array. otherwise disp(text_v(pos(1))) will display the information from the first column only.

In short, is there a way to get the legend index?


for example, if the for the values is:

0.1 0.2 0.4
0.5 0.7 0.6
0.8 0.9 0.0

and the corresponding text information is:

A B C
D E F
G H I

then the resulting graph should have three lines and when I click on 0.2, B should be displayed in the command window

1

There are 1 best solutions below

0
On

In order to get the display the value in text_v, you first need to find the index in the values matrix that matches with the selected value in pos.

pos = [..., ...];
disp(text_v(values(:)==pos(2));