Faster way to update a plot?

5 views (last 30 days)
Leo Müller
Leo Müller on 18 Nov 2015
Answered: Chad Greene on 3 Jan 2016
Hello dear community.
I have the following problem:
My program creates a two different arrays (vectors of different lengths) in each step of a for-loop. Right now I am plotting these two vectors within the loop which makes it update each time the loop is completed. Unfortunately the plotting seems to have a great impact on the speed of my program. I would like to ask for advice to speed up my loop.
Thank you for your help!

Accepted Answer

Chad Greene
Chad Greene on 3 Jan 2016
Do you need to plot inside the loop? The best thing you can do is wait on plotting until after the loop. For example,
hold on
for x = 1:1000
plot(x,sind(x),'bo')
end
will take much longer than
x = 1:1000;
y = NaN(size(x));
for k = 1:length(x);
y(k) = sind(x(k));
end
plot(x,y,'bo')
If you can remove the loop entirely, that will be fastest:
x = 1:1000;
y = sind(x);
plot(x,y,'bo')
Experiment with the above. Put tic before a section and toc after to see how long it takes your computer to run a given section.

More Answers (0)

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!