pause vs drawnow in app designer

Hi folks,
in my current function I used drawnow to update a figure in a while loop. But drawnow seems to slow down the programm (there are lots of topics on this). I know about the drawnow limitrate, but this command doesn't work with Matlab Compiler (figure begins to blink when compiled).
So today i tried to use the pause command instead of drawnow. This seems to speedup the code and the redrawing of the figure but all the interactivity of callbacks get's lost (needs lots of time!!!).
Is there anything else I could try to increase the performance of my code?

1 Comment

What about using pause() every n-th (say, n=5) iteration.

Sign in to comment.

Answers (2)

I use drawnow in combination with the pause of 0.1s. I did not experiment with the values of pause.
while
%code
drawnow
pause (0.1)
end
They don't get lost, it's just that they get queued because program waits as it has been told.

12 Comments

The combination of both commands doesn't seem to increase the performance. I already tried this combination. I also tried:
pause(0.005);
drawnow;
pause(0.005);
Or pause first and drawnow second. Didn't make any changes. The only thing increasing performance is pause(0.005) without drawnow but the problem is as in the description above.
No need for the second pause, try increasing it to 0.01, as 0.005 is super low.
I found out if I plot on UIAxes instead of a seperate figure drawnow limitrate works fine. No blinking of the figure after Compiler. So limitrate is an option which improves the performance. pause() even if it's 10ms isn't changing performance in combination with drawnow.
Figure blinking might be the issue with the focus or when you use uigetfile/uigetfolder etc.
Due to the fact that you had two figures, drawnow might focus one and then the other causing the blink. I am not familiar with the details what that function actually does.
Dominik's comment:
no I only had one figure all the time. But if I plot on a seperate figure instead of an UIAxes the figure blinks if I use drawnow limitrate.
Now I get to another point: Is it possible that drawing on an UIAxes is less efficient than drawing on a common figure?
Updating figure in while loop might be heavy on the resources, can you show example of code you're using within while loop. This might be also useful for your plots refreshdata.
h = animatedline(app.UIAxes);
axis(app.UIAxes,[0,4*pi,-1,1])
x = linspace(0,4*pi,1000);
y = sin(x);
nToc = 0;
toc_calc = 0;
for k = 1:length(x)
tic
addpoints(h,x(k),y(k));
drawnow
nToc = nToc + 1;
toc_calc = toc_calc + toc;
end
avgTime = toc_calc/nToc;
app.averagetimeLabel.Text = 'average time: ' + string(avgTime)+ 's';
this is a simple example based on the help page for animatedline. If you want to see the difference between them remove 'app.UIAxes' in line 1 and line 2. the for loop is basecally replacing the while loop.
Edit: without app.UIAxes (plotting on common figure) the code runs about 4 times faster than drawing on UIAxes
Edit2: I found the following link which could be interesting:
Appdesigner (or specifically UIFigure) employs a HTML/Javascript layer which is less efficient than the standard matlab figure.
I checked the CPU usage of both code versions. The one drawing on UIAxes increases the CPU usage about 20-40% compared to the one drawing on the common figure.
Instructive. Comparison like this comforts me that I should stay on GUI rather than switch to app Designer
What is purpose of addpoints? The way it is written is similar to plot function which will have same or maybe better performance. drawnow refreshes all figures, even having one is an issue if you would like to have it refreshed dozens of times.
for k = 1:length(x)
tic
addpoints(h,x(k),y(k));
drawnow
nToc = nToc + 1;
toc_calc = toc_calc + toc;
end
There are some examples on animatedline documentation to speed up animation, by adding more than one point at a time and so on.
You can also try by setting the XData and YData properties of UIAxes (not sure if this is possible with animatedline) within the loop by adding the elements together, that way you avoid the plot function, and only use drawnow to refresh data.
Profiler is a neat way to test performance of what you have written, check it here.
Bruno Luong
Bruno Luong on 13 Oct 2020
Edited: Bruno Luong on 13 Oct 2020
Under the hood of animatedline is perhaps set x, y data with better handling of FIFO graphical data points.
I try to replace with SET, it does not get any visible faster update, and the conclusion is the same, updatinf plot (happens with drawnow is 3-4 time slower on UIAxes than regular axes.
Regardless the conclusion of the test here is that drawing on uiaxes has poor performance. There is nothing end user can do about it.
Actually it's not the uiaxes, it's the issue with uifigure. What exactly with it, I wouldn't know.
fig = figure; % 0.124s
ax = axes(fig);
fig = figure; % 0.152s
ax = uiaxes(fig);
Bruno Luong
Bruno Luong on 13 Oct 2020
Edited: Bruno Luong on 13 Oct 2020
Confirmed, I cross check,
matlab.graphics.axis.Axes in UIfigure (app designer): slow 0.0303
ax = axes(figure): fast 0.0071
ax = uiaxes(figure): fast 0.0079

Sign in to comment.

Dominik Müller
Dominik Müller on 13 Oct 2020
no I only had one figure all the time. But if I plot on a seperate figure instead of an UIAxes the figure blinks if I use drawnow limitrate.
Now I get to another point: Is it possible that drawing on an UIAxes is less efficient than drawing on a common figure?

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Asked:

on 13 Oct 2020

Edited:

on 13 Oct 2020

Community Treasure Hunt

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

Start Hunting!