Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 946
Stack Player Avatar asked May 01 '17 14:05

Stack Player


1 Answers

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
like image 198
m7913d Avatar answered Oct 21 '22 22:10

m7913d