Continuous tracking of 'LocationChanged' event of figure

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?

 Accepted 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]));

4 Comments

That is a nice solution.
Is there a resource listing possible events?
A google search (albeit a short one) returned for both:
site:www.mathworks.com LocationChanged
and
matlab LocationChanged
this thread as top result and nothing resembling an official resource.
Thank you in advance.
Hi Sean, Thanks, that was basically my approach. Unfortunately, it doesn't work. Following Kai's comment below, it would seem that the problem is related to my OS. Or java installation (Java 1.7.0_75-b13). Or who knows... I'll check on different systems and try to get more data points.
@Kai, et al. you can look at the meta class for any class to see all of the attributes of all events/methods/properties for any class.
I just did:
mc = metaclass(figure)
Strange, though, that the events() command lists such a small subset of the events:
>> events(gcf)
Events for class matlab.ui.Figure:
ObjectBeingDestroyed
PropertyAdded
PropertyRemoved
That would seem to mean that 'LocationChanged' and others are undocumented and aren't to be relied upon for backward compatibility.

Sign in to comment.

More Answers (1)

If you are just trying to display two plot side by side the subplot function is what you are looking for, without having to track figure locations.
If you really want to move the individual figure you can achieve this with:
function window_motion_test
mainFig = figure();
pause(0.2) % Wait for the figure construction complete.
jFig = get(main_fig, 'JavaFrame'); % get JavaFrame. You might see some warnings.
jWindow = jFig.fHG2Client.getWindow; % before 2011a it could be `jFig.fFigureClient.getWindow`. Sorry I cannot test.
jbh = handle(jWindow,'CallbackProperties'); % Prevent memory leak
set(jbh,'ComponentMovedCallback',{@windowMoved});
followFig = figure();
function windowMoved(src,callbackdata)
jComponent = callbackdata.getComponent;
mainFig_pos = jComponent.getLocation;
mainFig_size = jComponent.getSize;
followFig_width = followFig.Position(3);
followFig_height = followFig.Position(4);
followFig.Position = [mainFig_pos.x + mainFig_size.width,...
mainFig_pos.getY,...
followFig_width,...
followFig_height];
%followFig lower left corner is now attached to
%mainFig lower right corner
end
end
The important part comes from this question on stackoverflow, which in turn references this entry on undocumentedmatlab.

4 Comments

Thanks, Kai. Subplot will not work, the figures that I want to synchronize are rather complex GUIs. The function window_motion_test does not work smoothly, it behaves exactly as my code: while I'm dragging figure a, figure f will not move till the end. So, I don't see f and a moving at the same time, but f jumping to the side of a when I'm finished dragging a. Do you also see this behaviour, or does figure f smoothly follow figure a in your system?
What version of Matlab are you using?
I am using 9.3.0.713579 (R2017b) and it produces the wanted result of a smoothly following figure.
Oh, interesting... I'm using R2017a on a Mac. I'll try to get a 17b version and try...
well, I just did. It definitively doesn't work in my laptop with any matlab version (2017b,2018a), so maybe it is something system dependent (I'm using OS X Yosemite)... I'll try on windows or linux machines when I have the occasion...

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 27 Mar 2018

Commented:

on 18 May 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!