Search code examples
matlabplotinteractivemarkerclickable

Make clicking MATLAB plot markers plot subgraph


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?


Solution

  • 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.