MATLAB crashes when saving figures in a loop

3 visualizaciones (últimos 30 días)
Eric Foote
Eric Foote el 30 de Jul. de 2018
Comentada: Eric Foote el 31 de Jul. de 2018
I haven't been able to find any other answers that address this specifically. I'm trying to write a script that generates a bunch of figures and saves them as images, and since there's thousands of images to generate, I'm doing it in a loop.
I've created a simplified script that still reproduces the problem I'm having:
for iteration = 1:1000
% Generate some data
x = 1:100;
y1 = randi(100, 1, 100);
y2 = y1 + 1;
% Plot the data
figure
hold on
plot(x, y1, '-r');
plot(x, y2, '-b');
% Save an image
filename = ['iteration' num2str(iteration)];
saveas(1, [filename '.png']);
% Close the figures
close all
end
The above code usually crashes with a Segmentation Violation somewhere around iteration 15 or so.
Any idea what would be causing this to crash? Is there a preferable way to do what I'm trying to do here?
  3 comentarios
Eric Foote
Eric Foote el 30 de Jul. de 2018
I just checked that-- I have 80GB free, but it seems like that should be enough since each image is only around 34kB
KALYAN ACHARJYA
KALYAN ACHARJYA el 30 de Jul. de 2018
Editada: KALYAN ACHARJYA el 30 de Jul. de 2018
Its perfectly works in my case. Have you try to save the image as jpg, this not a strong reason, maybe it works. You can try
  1. #Clear the workspace
  2. #Exit the Matlab and restart again
  3. #First Try 100, if it works then only go for 10000
There are only hit and trials options available to you.
Hope it helps

Iniciar sesión para comentar.

Respuestas (1)

OCDER
OCDER el 30 de Jul. de 2018
Try updating your graphics card driver.
Also try this for debugging purposes. If an error occurs, do you see "plotted ok" or "saved ok" as the final message?
for iteration = 1:10
x = 1:100;
y1 = randi(100, 1, 100);
y2 = y1 + 1;
FH = figure('visible', 'off'); %<=visible off. Tracking figure handle as FH.
hold on
plot(x, y1, '-r');
plot(x, y2, '-b');
disp('plotted ok'); %Debugging, no message = error in plotting
saveas(FH, sprintf('iteration%d.png', iteration)); %Using sprintf
disp('saved ok'); %Debugging, no message = error in saving
close all
end
Or try the "painters" renderer when saving. Replace the saveas with print.
print(FH, sprintf('iteration%d.png', iteration), '-dpng', '-r300', '-painters');
  1 comentario
Eric Foote
Eric Foote el 31 de Jul. de 2018
It's possible that setting visible to 'off' may have fixed things-- it seems to have kept things from crashing at least in the simple test program I made. I'm going to see if that works with what I was originally trying to do.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by