3

In Matlab 2011b, I have a multidimensional matrix which is to be initially presented as a 2D plot of 2 of its dimensions. I wish to make the markers clickable with the left mouse button. Clicking on a marker draws a new figure of other dimensions sliced by the clicked value.

This question is related to Matlab: Plot points and make them clickable to display informations about it but I want to run a script, not just pop up data about the clicked point.

Googling hinted that ButtonDownFcn could be used, but examples I found require manually plotting each point and attaching a handler, like so:

hp = plot(x(1), y(1), 'o');
set(hp, 'buttondownfcn', 'disp(1)');

As there are many markers in the main graph, is it possible to just attach a handler to the entire curve and call the subgraph-plotting function with the index (preferable) or coordinates of the marker clicked?

Community
  • 1
  • 1
Gnubie
  • 2,587
  • 4
  • 25
  • 38

1 Answers1

3

this is an idea of what you need, and should help get you started if I understand your requirements.

In this case, when you select a curve, it will draw it in the bottom subplot preserving the color.

function main
subplot(211)
h = plot (peaks);

set (h,'buttondownfcn', @hitme)
end

function hitme(gcbo,evendata)
subplot (212)
hold on;

col = get (gcbo,'Color');
h2 =  plot (get (gcbo,'XData'),get (gcbo,'YData'));
set (h2,'Color', col)

pt = get (gca, 'CurrentPoint');
disp (pt);
end

You can explore your options for get by simply writing get(gcbo) in the hitme function.

Rasman
  • 5,349
  • 1
  • 25
  • 38
  • Thanks. Though your example shows how I can get a 2D slice of 3D data, I am more interested in getting the index of the marker of a 2D graph that I click on. For example, modifying your code below, clicking any marker prints the same XData and YData. I would like to get the index (1, 2, 3, etc) of the marker clicked instead. function main data = peaks; h = plot (1:size(data,2), data(1,:), 'o-'); set (h, 'buttondownfcn', @hitme) end function hitme (gcbo, evendata) get (gcbo, 'XData') get (gcbo, 'YData') end – Gnubie Apr 24 '12 at 12:50
  • if all you want is the point, add `pt = get (gca, 'CurrentPoint');`, as shown above. It will give you a 3D point, so depending on your need, just use the first row. – Rasman Apr 24 '12 at 13:42
  • Many thanks! It gives me the coordinates, rather than the index of the marker, but since I know the original matrix, I can calculate the index from the x-coordinate e.g. round(pt(1)) . – Gnubie Apr 25 '12 at 16:18