当鼠标使用 matlab 移动时,从图中检索数据点



我在同一图中绘制了多个子图。当我将光标移动到图上方时,我想从每个子图读取值,而不是在每个子图上手动插入"数据点"。

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

Matlab 中是否有任何内置函数可以做同样的事情......谢谢

ginput 可能是你要找的。

下面是一个示例。希望您可以调整它以做您想做的事。

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

为了实现您要查找的内容,您可能需要在图形属性中定义WindowButtonMotionFunction回调。每当移动鼠标时都会触发回调,然后从鼠标位置(当前轴的currentPoint)更新已创建的任何数据查看器。

为了您的阅读乐趣,这里帮助WindowButtonMotionFunction出来的帮助:

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.

最新更新