Is it possible to synchronize data cursors across multiple figures in MATLAB?
Asked Answered
F

1

12

Is it possible to create a data cursor on a given figure at a certain location and then link other cursors in other figures to it? (not subplots)

The objective is that when I move the location of one of these cursors manually, all other data cursors in each of the other figures move to the same location in parallel with it (assuming all figures are of same size).

Farmann answered 1/5, 2017 at 14:45 Comment(1)
Why are you asking specifically about other figures? Is there an easier way to do it with subplots of the same figure?Coccus
S
9

It is possible using undocumented MATLAB functions. The trick is to catch when a datatip is changed and update the others accordingly.

A proof of concept with two linked plots is shown below:

% 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
Schnabel answered 1/5, 2017 at 17:6 Comment(1)
Thank you very much for your answer. In case someone needs this for the 2D image synchronization purpose, change a few lines. p1 = plot(1:10) >> p1 = imshow(im1) and p2 = plot(1:10) >> p2 = imshow(im2) and add d.Position(2) = pos(2) below d.Position(1) = pos(1).Repetition

© 2022 - 2024 — McMap. All rights reserved.