我在同一个图中绘制了多个子图。当我将光标移到图上方时,我想从每个子图中读取值,而不是在每个子图中手动插入“数据点”。
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
中定义-callback 。每当您移动鼠标时,回调就会触发,并且从鼠标位置(当前轴的),您可以更新您创建的任何数据查看器。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.