Question

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

Was it helpful?

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)])

OTHER TIPS

To achieve what you look for you may want to define the WindowButtonMotionFunction-callback in the figure properties. The callback fires whenever you move the mouse, and from the mouse position (currentPoint of the current axes), you can then update whatever data viewer you have created.

For your reading pleasure, here the help to the WindowButtonMotionFunction out of the help:

WindowButtonMotionFcn
function handle | cell array containing function handle and additional arguments | string (not recommended)

Mouse motion callback function. Executes whenever you move the pointer within the figure window. Define the WindowButtonMotionFcn as a function handle. The function must define at least two input arguments (handle of figure associated with key release and an event structure).

See Function Handle Callbacks for information on how to use function handles to define the callback function.

Example Using All Window Button Properties

Click to view in editor — This example enables you to use mouse motion to draw lines. It uses all three window button functions.

Click to run example — Click the left mouse button in the axes and move the cursor, left-click to define the line end point, right-click to end drawing mode.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top