MATLAB: Continuous tracking of ‘LocationChanged’ event of figure

figure eventlocationchanged

Hi,
I am trying to glue a (main) figure 1 to a (following) figure 2, so that when the user manually moves the main figure, the following figure moves with it. The intended effect is that they are side by side at every moment. By now, my strategy is to use a listener that listens to the 'LocationChanged' event of main figure and replaces figure 2 according to the changes notified by figure 1. The problem is that 'LocationChanged' seems to get notified only after the user has stopped moving the main figure. At this moment, figure 2 just jumps to the side of the main figure…. is there a way to ensure that the event 'LocationChanged' is issued continuously while the figure is being moved?

Best Answer

It seems to be firing and responding correctly for me (18a, Windows) as I drag fig1 around.
posOffset = [500 0]; % positional offset
fig1 = figure('Units','pixels','Position',[140 140 400 400]);
fig2 = figure('Units','pixels','Position',[fig1.Position(1:2)+posOffset 400 400]);
listener1 = event.listener(fig1, 'LocationChanged', @(~,~)set(fig2, 'Position', fig1.Position+[posOffset 0 0]));