Matlab slows downs within certain loop
Show older comments
In my code I have the following loop where running my code slows down:
for i = 1:c_max
c_n = cm(i,:);
x = c{i}(:,1);
y = c{i}(:,2);
z = c{i}(:,3);
markersize = c{i}(:,4);
for j = 1:size(c{i}(:,1),1)
splot = scatter3(x(j),y(j), z(j),markersize(j), c_n);
drawnow limitrate
end
end
I know that when using "drawnow" it makes the simulation slower. However, when I'm not using it, somehow my plot and matlab do not respond anymore after the plot is shown and I have to restart matlab again. I'm also unable to save the plot because matlab does not respond anymore. Does any one have suggestions on how to make it faster and/or not make matlab unresponsive? In this case, I'm using 250k data points within each column matrix 'c' and the maximum value for 'c_max' is 25.
13 Comments
Adam Danz
on 13 May 2019
I formatted your code. In the future, please use the code format button when you provide code.
Stephen23
on 13 May 2019
"Does any one have suggestions on how to make it faster and/or not make matlab unresponsive?"
Update the line/patch/... data, rather than calling plot/scatter/... on every iteration.
mootje_3000
on 13 May 2019
Edited: mootje_3000
on 13 May 2019
Adam Danz
on 13 May 2019
If you're not using splot (output to scatter3), just remove that. It might help a tad.
mootje_3000
on 13 May 2019
Adam Danz
on 13 May 2019
I assume somewhere in the code prior to the i-loop you are using hold on to make sure that you are not overwriting the axes on each loop, right?
mootje_3000
on 13 May 2019
Adam Danz
on 13 May 2019
"hold on" prevents the axes from clearning when you add more objects to the plot.
Walter Roberson
on 13 May 2019
Do you need to see the display evolve or is that more to check that work is proceeding properly?
All of those graphics objects together is inefficient. You could use one total graphics object or slightly easier one per i value, if you update the xdata ydata and size data properties instead of creating all those objects
Adam Danz
on 13 May 2019
Note that you'll also need to update the markersize and color values along with the X/Y/ZData values. Fortunately scatter3() allows vectors as size inputs and matrices as color inputs where you can specify the size and color of each (x,y,z) coordinate.
What is stored in each C{i} element? Is it a vector? A matrix? Are they all the same size? If the answer to those questions are 'yes', I have an obvious solution to speed things up.
Walter Roberson
on 13 May 2019
I noticed that all items in the same i use the same color. If one scatter() object were to be used per i then the color data could be left as a 1 x 3 vector instead of having to be repmat() of that to the number of rows equal to the current number of objects.
The user has indicated that c is max 25 elements, and that each element is a matrix of up to 250k rows.
There are ways to paint all of this in bulk, but with the drawnow in there, I have to suspect that they want to view the simulation evolve.
mootje_3000
on 13 May 2019
Edited: mootje_3000
on 13 May 2019
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Performance in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!