在MATLAB中,是否可以同步多个图形之间的数据光标?

12

是否可以在给定的图形上创建数据光标并将其他图形中的光标链接到它?(不是子图)

目标是,当我手动移动其中一个光标的位置时,所有其他图形中的数据光标都会平行移动到相同的位置(假设所有图形的大小相同)。


你为什么特别询问其他图形?使用同一图形的子图有更简单的方法吗? - laugh salutes Monica C
1个回答

9

可以使用未公开的MATLAB函数实现。诀窍在于捕获数据提示更改的时机,然后相应地更新其他内容。

下面展示了两个链接图的概念证明:

% first plot
f1 = figure;
p1 = plot(1:10);
datacursormode on; % enable datatip mode
c1 = datacursormode(f1); % get the cursor mode
d1 = c1.createDatatip(p1); % create a new datatip

% second plot
f2 = figure;
p2 = plot(1:10);
datacursormode on;
c2 = datacursormode(f2);
d2 = c2.createDatatip(p2);

% register the function to execute when the datatip changes.    
set(d1,'UpdateFcn',@(cursorMode,eventData) onDataTipUpdate(cursorMode,eventData, d2))
set(d2,'UpdateFcn',@(cursorMode,eventData) onDataTipUpdate(cursorMode,eventData, d1))

% callback function when the datatip changes
function displayText = onDataTipUpdate(cursorMode,eventData, d)
   pos = get(eventData,'Position'); % the new position of the datatip
   displayText = {['X: ',num2str(pos(1))], ...
                      ['Y: ',num2str(pos(2))]}; % construct the datatip text
   d.Position(1) = pos(1); % update the location of the other datatip.
end

1
非常感谢您的回答。如果有人需要用于2D图像同步目的,请更改几行代码。将p1 = plot(1:10)更改为p1 = imshow(im1),将p2 = plot(1:10)更改为p2 = imshow(im2),并在d.Position(1) = pos(1)下方添加d.Position(2) = pos(2) - Prefect

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接