Why is it so slow to draw a figure in Matlab?
Show older comments
I just started learning Matlab.
I am drawing very simple function curves, like y=kx or y=sin(fx).
Then I am using a slide bar to change the values of k or f.
In softwares like Geogebra, or program I made using VB.net, the figure will change with the changing values without any delay.
But in Matlab, though the figure does change, there is an obvious delay, it seems the drawing takes a lot of time.
In Matlab, I tried to draw using Script, Live Script, App.
While changing the k or f values, I have tried to update the figure with 'plot' or 'fplot' directly, or to update with 'set' to change the 'yData' or 'Function'.
How can I speed up the drawing in Matlab?
What's causing this slow drawing?
12 Comments
Walter Roberson
on 28 May 2022
Sometimes that can be caused by having poor graphics drivers. Are your drivers up to date?
smarthu
on 28 May 2022
Walter Roberson
on 28 May 2022
What shows up for
opengl info
smarthu
on 28 May 2022
Walter Roberson
on 28 May 2022
https://www.intel.com/content/www/us/en/download/19344/intel-graphics-windows-dch-drivers.html
You are a little out of date on your graphics driver; I linked to the current installer.
Jan
on 28 May 2022
It would be much easier to find problems of your code, if we can see the code. Maybe you create a new figure in each iteration, loose time with clf or cla etc. Please post your code to give us a chance to help.
smarthu
on 28 May 2022
Jonas
on 28 May 2022
did you try to add
drawnow limitrate;
directly after the plot command?
smarthu
on 28 May 2022
fplot is a user-convenience function, not one built for performance. It does everything all over every time it is called; I wouldn't expect it to be very fast...easy, yes; high-performance, "not so much".
To get any speed here I think you'll need to create the graph/axes and directly recompute the ydata and replace it in the line handle or use line instead of plot
I also suspect it's the latency of the callbacks from the app contributing to more of the delay than the actual drawing; not sure there's much that can be done there. Or, there may be so many callbacks generated by moving the slider it overwhelms the compute power -- that's what the 'limirate' parameter on drawnow will take care of by throwing away a bunch of them. Whether there's any way to increase the granularity or reduce the number of callbacks on how much of a variation it takes to trigger a callback I don't know.
I'm guessing although I've done almost nothing with the app GUI overall (and what I have done have nothing that has anything except static updates of input parameters like file name selection) so I just don't have any hands on experience that is truly applicable, but my guess here is one is seeing the inherent overhead in MATLAB.
There was another similar topic not too long ago -- an attempt to measure time a user kept the cursor inside a graphic area on the screen -- it was easy to move the mouse sufficiently fast to completely miss the event -- some machinations of the code as far as how it was done helped considerably, but it never got to be under a fair latency time. I dunno if I could find that topic again now or not...
<Answer_864425> is a demo of the above subject that shows 65 msec average latency to the response above -- the discussion there shows some "tricks" that were done from OPs original implementation that helped the response there; it was, of course reading the cursor position of a fixed graphic but it did have to compute the inside/outside decision. What would be a response time of a graphic update in that case I wonder...how much time would the graphics take as opposed to the latency?
smarthu
on 29 May 2022
dpb
on 30 May 2022
Ah, so! I was certain it would help, not so sure it would be a total solution...
Answers (1)
Hi,
To my understanding, you are attempting to draw function curves with a parameter whose value depends on a slide bar.
Here is the possible workaround:
You can create Live Editor in MATLAB and divide the whole code into 2 sections:
- Part 1: Where setup of the entire graph is placed
- Part 2: Where actual changing of parameter and plotting of graph is placed
It will look like:
Section 1
%% Sine Wave plot
%% Time specifications:
Fs = 8000; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.25; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%% F and x specifications:
Fc = 60;
x = 2*pi*Fc*t;
Section 2
k = %% this represents Numeric Slider in Live Task
k
-7
y = sin(k*x);
% Plot the signal versus time:
figure;
plot(t,y);
xlabel('time (in seconds)');
title('Signal versus Time');
zoom xon;
By doing so,
If you change the 'k' value using slider, only graph and plot part will get affected and thus will update the graph fast.
On a similar note, if you want to just make a small part of code dynamic then try to keep it in a separate section in live editor to get faster execution.
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!