MATLAB Answers

App Slow With UIAxes

117 views (last 30 days)
Hi. I'm creating apps on app designer which use components such as sliders to control a plot. When plotting on a UIAxes, there is significant delay. However, when having the app plot on a separate figure, there is no delay. Does anyone know the reason for this and if there's a way to plot on the UIAxes without the delay? Thanks.
I'm going to use an app modelling a cylinder removing a lot of the additional unnecessary stuff, so the type of problem is as clear as possible. The plot consists of surf, patch, and plot3. For all apps, the goal is not plotting data per say, but rather creating shapes, often in 3 dimensions. Therefore, the data is generally arrays of type double which don't generally get over 100 elements.
I'm using MATLAB R2020a.
These are the two methods:
  • I drag and drop a UIAxes on to the app and plot on that axis. This takes about 3 seconds to update the plot each time I move the slider. This is a screenshot of the setup:
  • I create a separate regular figure (not uifigure) and plot it on that. This has almost no delay:
This is the code used to create the separate figure and have them aligned neatly (I don't think it's that important but it clarifies what I'm doing):
global ax %needed so it can be accessed through component callbacks
divide=.2;%the fraction of the screen filled by the app
set(0,'units','pixels')
pixels=get(0,'screensize');
app.UIFigure.Position=[0,0,divide*pixels(3),pixels(4)];
fig=figure; %new figure
fig.Position=[divide*pixels(3),0,(1-divide)*pixels(3),pixels(4)];
ax=axes(fig); %axis it will be plotted on
Here is the code which I used (I removed much of the code used in the original app to simplify things) (the code is based off of Clay M. Thompson's cylinder function, and I kept the copyright text in the code):
function torsion(ax,ax2,M,G,L,r,display_in)
% Clay M. Thompson 4-24-91, CBM 8-21-92.
% Copyright 1984-2002 The MathWorks, Inc.
cla(ax)
% engineering equations:
J=1/4*r^4;
phi=M*L/(J*G);
% set up cylinder:
n = 50;
r = [r r]';
r = r(:); % Make sure r is a vector.
m = length(r); if m==1, r = [r;r]; m = 2; end
theta = (0:n)/n*2*pi;
sintheta = sin(theta); sintheta(n+1) = 0;
x = r * cos(theta);
y = r * sintheta;
z = (0:m-1)'/(m-1) *L* ones(1,n+1);
hold(ax,'on')
% plot cylinder
surf(ax,x,y,z,'EdgeColor','none','FaceAlpha',1)
patch(ax,x(1,:),y(1,:),z(1,:),[.25 0 .7])
patch(ax,x(1,:),y(1,:),z(2,:),[.25 0 .7])
%plot helices
z_vals=linspace(0,L,n*L/(2*pi*r(1)));
for i=0:n-1
angle0=i*(2*pi)/n;
anglef=angle0+phi;
theta_part=linspace(angle0,anglef,length(z_vals));
x_part=(r*1.01)*cos(theta_part);
y_part=(r*1.01)*sin(theta_part);
z_vals=linspace(0,L,length(x_part));
plot3(ax,x_part,y_part,z_vals,'k','LineWidth',.5)
end
% plot circles
for i=z_vals
plot3(ax,x,y,i*ones(1,n+1),'k','LineWidth',.5)
end
end

  10 Comments

Show 7 older comments
Adam Danz
Adam Danz on 6 Aug 2020
@Ephraim Bryski & @Chris Portal, I timed the startupFcn which does all of the plotting and some other stuff (much of which is suboptimal) and compared the timing when an embedded uiaxis is used within the app and when the external figure and (regular) axis is created. This was repeated 10 times. Results are below. Note that the app-uiaxes are created prior to the tic-toc time but the external figure and axes are created within the tic-toc time. Nevertheless, the independent axis version is ~7x faster (MATLAB Version: 9.7.0.1296695 (R2019b) Update 4).
I ran the profiler on the startup function using the app's uiaxis which shows the greatest Self Time within UIAxis.set.View.
Ephraim Bryski
Ephraim Bryski on 6 Aug 2020
Thank you so much! I couldn't figure out how to run and time for an app. How were you able to do this?
I don't really understand what UIAxes>UIAxes.set.View is or how to optimize it. Do you think there's an way to speed it up? I also would guess that this might only be called in the startup function and not for each callback, in which case speeding it up wouldn't actually make it faster when using the app.
Adam Danz
Adam Danz on 6 Aug 2020
A simply way to time a section of code is by using tic and toc (check out the documentation).
Example
tic
x = linspace(-pi,pi,180);
y = sin(x);
plot(x,y)
toc
The timing report was produced using profile. Again, see documentation.
profile on
% DO STUFF
profile viewer
Most of the stuff in the timing report I shared, including the line I highlighted, is stuff happening behind the scenes. You can't optimize much of that. However, there's a few suboptimalities in your startupFcn. I've shut down Matlab for the day so I can't view the code now but I did highlight two items in my first comment in this thread. I also recall seeing two axes being created but only one was used.

Sign in to comment.

Accepted Answer

Chris Portal
Chris Portal on 7 Aug 2020
Thank you @Adam Danz and @Ephraim Bryski. The additional info is useful. I see the same uifigure/uiaxes slowness Adam is showing.
Ephraim, try using a uifigure/axes combination instead. This shows similar performance results to the figure/axes combination:
  • UIFIGURE/UIAXES: Elapsed time is 1.871439 seconds.
  • UIFIGURE/AXES: Elapsed time is 0.205658 seconds.
  • FIGURE/AXES: Elapsed time is 0.204895 seconds.
A few notes:
  • UIAXES is a heavier weight version of AXES (it's effectively an axes with an invisible UI panel). This makes it slower, although the amount of slowness here is a little surprising. I'll report this to the development team to investigate.
  • The axes you get in App Designer when you drag and drop from the palette is always a UIAXES. So in order to use an AXES with your UIFIGURE, you'll need to manually create and position it as part of your startup function.
  • Lastly, these UIAXES/AXES differences will soon become a much simpler story with some up and coming work. Keep an eye out for the MATLAB release notes!

  3 Comments

Florin
Florin on 30 Sep 2020
Hello,
Are the app-uiaxes issues solved with 2020b? (reading in the relase notes:'Also, UIAxes objects now have the same properties and options as Axes objects have for customizing their appearance and behavior:')
I am using this method: https://www.mathworks.com/help/matlab/ref/uigridlayout.html#mw_9f162f16-28ed-46bb-8bfa-cbd2efd0d143 to show/hide some grids in App Designer containing uiaxes and uitable and there is a noticeable lag while hiding or showing the grid. The lag is mostlty comming from 'clipping' and resizing the axes or table (do not confuse with axes clipping property!)
I tried to hide the grid childred while folding (setting RowHeight(i) = {0}) or unhide while unfolding (RowHeight(i) = {'2x'}) but it doesnt seems to bring any visual improvement for the app
Thanks!
Mario Malic
Mario Malic on 29 Oct 2020
It still is an issue, and is probably being fixed. My issue is with 3D rotation, it takes around 3-7 seconds to even start moving. I have attached code and variables to test.
Alberto Lutman
Alberto Lutman on 21 Nov 2020
In 2020b it is still an issue.
How do I use the UIFIGURE/AXES combination?
I tried to use:
app.AX=axes('parent',app.UIFigure);
in the startup function, and then plot in app.AX, it is not faster than the UIAXES generated by app designer, and the FIGURE/AXES combination is still at least 7 times faster.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!