Search code examples
matlabplotmousedatapoint

Retrieving datapoints from the figure, when the mouse is moved about it using matlab


I have plotted multiple subplots in a same figure. When I move the cursor above the plot, I wanted to read the values from each subplot instead of inserting ‘Datapoint’ manually at each subplots.

A=[1 2 3 4 5 6];
B=[3 4 5 6 2 8];
C=[3 2 7 5 8 3];
figure(1);
subplot(2,1,1);
plot(A,B,'m')
hold on;
subplot(2,1,2);
plot(A,C,':y')
Title('Test figure')

Are there any inbuilt function in Matlab to do the same… Thanks


Solution

  • ginput might be what you're looking for.

    Here's an example. Hopefully you can adapt it to do what you want.

    xData = 11:20;
    data1 = rand(1, 10);
    data2 = rand(1, 10);
    
    ax1 = subplot(2, 1, 1);
    plot(xData,data1)
    hold on
    ax2 = subplot(2, 1, 2);
    plot(xData,data2)
    hold on
    
    [x,y] = ginput(1); % get one input point
    
    % plot and label that point in the first subplot
    axes(ax1)
    plot(x, y, 'ro')
    text(x, y, ['   x = ', num2str(x), 'y = ', num2str(y)])
    
    % label that point in the second subuplot
    axes(ax2)
    plot(x, y, 'go')
    text(x, y, ['   x = ', num2str(x), 'y = ', num2str(y)])