Very slow graphics when mouse pointer moves around a figure

15 次查看(过去 30 天)
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 个评论
Piero
Piero 2023-3-9
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.

请先登录,再进行评论。

回答(1 个)

Kelly Jantzen
Kelly Jantzen 2023-3-11
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 个评论
Kelly Jantzen
Kelly Jantzen 2023-3-13
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by