1

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

ducks13
  • 81
  • 2
  • 3
  • 10
  • 1
    use a `for` loop and substitute `1` by `ii` – ironzionlion Jul 17 '14 at 08:48
  • Then that will show the whole matrix. I don't want that I just want it to show the information associated with position. (see edit to clarify) – ducks13 Jul 17 '14 at 09:10
  • is `pos` an array? in that case, which size would it have according to your example? – ironzionlion Jul 17 '14 at 09:45
  • Sorry, I should have clarified this: the code above is edited from the text update function for a graph. Meaning that the function above is called when the graph is clicked at some position. for example, when clicked on the line at the position 0.2 then the pos value will be [2 0.2] since the X value will 2 and the graphed result is 0.2. – ducks13 Jul 17 '14 at 10:57
  • since `pos(1) = 2`, you want to display `'DEF'`, right? – ironzionlion Jul 17 '14 at 11:23
  • well, pos(1) just gives the row information. so when pos(1) = 2 then it will only display D. This means is I click on 0.5 ,0.7 or 0.6 in the graph (any number from the second row) I will get D. Instead, I want to get D if I click on 0.5, E if I click on 0.7 and F if I click on 0.6. – ducks13 Jul 20 '14 at 05:22

1 Answers1

0

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));
Rohan
  • 45
  • 5
ironzionlion
  • 832
  • 1
  • 7
  • 28