Very slow graphics when mouse pointer moves around a figure
15 views (last 30 days)
Show older comments
I've got a little widget that animates a sequence of images by calling the "image()" function repeatedly with new images in a sequence. Normally the animation proceeds reasonably fast around 50-60 frames per second. However, if the mouse is in the figure window and moves around (not even clicking on anything), then the animation slows to a crawl (about 2-3 frames per second) as long as the mouse moves in the figure window. (See code at bottom for simple example to replicate this).
I've checked to ensure that the 'WindowButtonMotionFcn' figure callback is disabled (as are all other figure callbacks). I can't find any other figure, axes or even image objects that might be triggered by mouse motion in the figure window.
If I run the profiler and compare results with and without the mouse moving around the figure window, I see the function:
ToolbarController.ToolbarController>@(e.d)obj.handleMouseMotion
(and its dependent functions ) is getting called thousands of times and is consuming 90% of the runtime when the mouse is moving around I tried turning off both the figure's toolbar and the figure's menu bar, e.g:
hFigure.ToolBar = 'none';
hFigure.MenuBar = 'none';
and that slightly alleviates the issue somewhat, but not much (perhaps 10-20% better) . Further, even with the toolbars turned off, the profiler still indicates that
ToolbarController.ToolbarController>@obj.handleMouseMotion
is still consuming 90% of the run time whenever the mouse is moving around. There also is the function
AxesToolbarButton.AxesToolbarButton>AxesToolbarButton.AxesToolbarButton
that is also getting called thousands of times and is consuming some of the time. So I also disable the AxesToolbar by:
axesTB = get(hAxes,'ToolBar');
axesTB.visible = 'off';
axesTB.BusyAction = 'cancel';
(Unlike the figure toolbar, there doesn't seem a setting to completely disable a AxesToolbar). Anyway, turning off the axesTB didn't seem to help.
% This code can be used to illustrate the issue of slowly updating graphics.
% Run this, and then move the mouse around the figure window.
for loop=1:500
image(rand(300,300),'CDataMapping','scaled');
drawnow;
end
%Clearing the axes with "cla" before the image() call doesn't change the result. However, this version of the code is seemingly NOT impacted by this issue or very little:
hImg = image(rand(300,300),'CDataMapping','scaled');
for loop=1:500
hImg.CData = rand(300,300);
drawnow;
end
In the latter case, the profiler say that the function
ToolbarController.ToolbarController>@(e.d)obj.handleMouseMotion
still gets called a whole bunch of times but it ends up only consuming about 15% of the runtime (not 90+%), and the animation proceeds at 50+ FPS. So there is something about the combination of a moving mouse and creating new objects (rather than changing attributes of existing objects) that seems to grind matlab graphics to a halt. While I clearly point out a workaround for my little example above, in the general case, such workarounds are not always possible. It seems odd that moving a mouse while drawing objects in a figure shouldn't necessarily slow down things as much as it appears to do.
Thoughts appreciated.
9 Comments
Piero
on 9 Mar 2023
I've a problem which probably has the same root. In my GUI I'm updating an image within an axes (also at a resaonable slow frame rate - 4 FPS) and if I hover it with mouse during such update, depending on the moment, it fails updating resulting in blank image. I'm not creating an object, but just changing its attribute .CData. All the underlaying data are safe, so I can't catch any issue to solve. I've also disabled axes Interactions and made axes toolbar not visible, but it didn't help.
Please help.
Answers (1)
Kelly Jantzen
on 11 Mar 2023
I had a similar problem while plotting data that was streaming in real time. Any movement of the mouse over the uiaxes interuppted the plotting. After searching online for how others addressed the issue, I found that the combination of uiaxes settings illustrated in the following code snippet worked perfectly for my application.
%
f = uifigure();
my_axis = uiaxes(f);
my_axis.XLimitMethod = 'tight';
my_axis.Interactions = [];
my_axis.PickableParts = 'none';
my_axis.HitTest = 'off';
I hope this works for you!
3 Comments
Kelly Jantzen
on 13 Mar 2023
BTW, I also found it necessary to initialize a single graphics object (an image in your case) prior to streaming and then update that objects properties during animation rather than creating a new object on each iteration. It speeds up plotting dramatically and (as you mention above) reduces teh impact of callbacks to mouse move events.
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!