4
votes

I have some points like this:

matrix = rand(6, 4)
0.8147    0.2785    0.9572    0.7922
0.9058    0.5469    0.4854    0.9595
0.1270    0.9575    0.8003    0.6557
0.9134    0.9649    0.1419    0.0357
0.6324    0.1576    0.4218    0.8491
0.0975    0.9706    0.9157    0.9340

the first two columns are x and y values which get plotted as points via

plot(matrix(:, 1), matrix(:, 2), '*r'

Now what I want to work out is the following: Whenever I click on a certain point in the plot, I want the information out of column 3 and 4 being displayed as text just right of the point in a box, e.g. with some text, e.g. information 1: VALUE_COL3, information 2: VALUE_COL4. How to achieve that? I thought of the ButtonDownFcn attribute and than check out the clicked point and match it. But is there any easier way to do it?

Thanks!

2

2 Answers

8
votes

While Sam's method is probably the correct solution here, I'd like to offer another one (although it is more of a 'hack' than a proper solution).

You can attach context menus to handle graphics objects. These menus can display multiple selections and even let your script respond to user selections. Take a look at the following example:

x = [1:10];
y = x.^2;

plot(x,y); hold on;
h = plot(x(5), y(5),'ro'); %% save the handle to the point we want to annotate

hcmenu = uicontextmenu;
item1 = uimenu(hcmenu, 'Label', 'info 1');
item2 = uimenu(hcmenu, 'Label', 'info 2');
item3 = uimenu(hcmenu, 'Label', 'info 2');

set(h, 'uicontextmenu', hcmenu);

When you right click on the 'o' point, you get the context menu:

Produces this...

More information can be found at the Mathwork's site.

6
votes

MATLAB figures have a feature called data cursors. On the toolbar, there's a button that looks like a a blue curve, with a crosshair above it and a little tooltip. If you click this and then select one of the points you plotted, you'll get a little tooltip above the point giving some information about that point. You can double-click on the tooltip to pick it up, and rag it across to other plotted points.

By default, the tooltip displays simple information about the points, namely their X and Y coordinates. But you can customise the text displayed to whatever you want, by getting a handle to the datacursormode object of the figure used for plotting, and setting its UpdateFcn. The UpdateFcn callback is executed to determine the text displayed on the tooltip - in your case it could get the corresponding values from the third and fourth columns of your matrix, splice them together with the string "information", and return that for display.

See this example in the documentation to see how that can be done in more detail.