Why does the PRINT command take so long with a scatter plot in MATLAB 7.5 (R2007b)?
6 views (last 30 days)
Show older comments
The PRINT command with the -djpeg flag takes a very long time to run. I have created a scatter plot with a large amount of data. I would like to export it to a jpeg but the print command takes a long time or freezes MATLAB completely.
Reproduction steps:
NumPoints=2000
x=[0:1:NumPoints];
sinx=0.25.*x.*sin(x);
cosx=0.25.*x.*cos(x);
scatter(sinx,cosx,'.');
tic
print -djpeg testspiral.jpg;
toc
Gives:
Elapsed time is 33.067874 seconds.
Compare that result with this one:
%
phmin=deg2rad(70);
phmax=deg2rad(50);
thmin=0;
thmax=2*pi;
NumPoints=200000
ph = phmin+(phmax-phmin).*rand(NumPoints,1);
th = thmin+(thmax-thmin).*rand(NumPoints,1);
[x y]=pol2cart(th,ph);
scatter(x,y,'.');
tic
print -djpeg myrand_cut.jpg
toc
which gives
Elapsed time is 13.166636 seconds.
Notice that the second example runs in less time with a data set that is 100 times larger.
For some reason the PRINT command has a particularly hard time with this type of cyclical data.
Accepted Answer
MathWorks Support Team
on 30 Aug 2013
Before the image is printed to file all of the elements that will appear need to be sorted. Depending on the data set this sort can take a long time.
To work around this issue try randomizing the order of the vectors that are passed to the SCATTER function.
For instance, if the data is contained in three vectors x, y and z instead of using
scatter3(x,y,z);
use
%Get the length of the input vectors.
len=length(x);
%Create a vector of random integers from 1 to len
rand_ind=randperm(len);
%Randomize the input vectors.
randx=x(rand_ind);
randy=y(rand_ind);
randz=z(rand_ind);
%Plot
scatter3(randx,randy,randz);
An alternative workaround can be to print in EPS format which uses vector graphics and hence is much faster. The EPS file can then be converted to JPEG using third party converters.
print -depsc2 -painters testspiral
0 Comments
More Answers (0)
See Also
Categories
Find more on Scatter Plots 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!