Matlab crashes with imwrite in loop
4 views (last 30 days)
Show older comments
I try to create a gif of a function changing over time. Therefore I plot the function and get the individual frames with the getframe function,
Plotting the function and creating a cell with the frames does not seem to be a problem. Only when I enter the loop with the imwrite function there seems to be a problem and the whole MATLAB Program just shuts down.
load('Fovertime.mat'); % loads cell array im
check_flag=0;
first = find(~cellfun(@isempty,im),1);
safename = 'Fovertime.gif';
for i = first:length(im)
[A,map] = rgb2ind(im{i},256);
% Write to the GIF File
if check_flag == 0;
imwrite(A,map,safename,'gif', 'Loopcount',inf, 'DelayTime', 0.2);
check_flag = 1;
else
imwrite(A,map,safename,'gif','WriteMode','append','DelayTime',1/15);
end
end
After crashing a gif file with roughly 25% of the full gif has been saved.
2 Comments
Theo Husby
on 22 Mar 2023
I have the same problem. It is intermittent in my case, sometimes running to completion, and other times running partway through the iterations of my loop. I'm also running 2021a.
Anton Semechko
on 4 Jul 2024
I just came across the same problem while generaing .gifs and saving them to a netwrok. I have never seen this issue while writing locally, so I suspect it might have something to do with the function being unable to acces the image file data while writing (?). Maybe the MathWorks wizzards can look into this??? What is bizzare is that Matlab just shuts down without any warning.
Answers (2)
Pranesh
on 12 Jul 2022
Hi ,
To my understanding your function stops execution without fully iterating through the loop.
A possible work around for this would be to remove the semicolon (;) in your first if statement.
if check_flag == 0
imwrite(A,map,safename,'gif', 'Loopcount',inf, 'DelayTime', 0.2);
check_flag = 1;
Thomas Bielser
on 25 Apr 2023
We are facing the same problem: Matlab crashes at different unpredicted places when writing animated gif images with the imwrite-command in a loop. Writing "jpg"-format works fine. A wait command in the loop is not practical and brings only moderate improvement. This error occurs on all our computers, both on internal drives and network drives. Here is our code for testing purposes:
% clear workspace
clear all
% image properties
image_x = 128;
image_y = 128;
image_n = 1000;
image_delay_s = 0.1;
% number of colors
color_n = 256;
% generate colormap
color_map = jet( color_n );
% generate image dataset
image = randi( [ 0, 255 ], [ image_x, image_y, image_n ], 'uint8' );
% initialize counter
loop_counter = 1;
% forever
while( 1 )
% output comment
disp( strcat( sprintf( "%04d", loop_counter ), ". iteration" ) );
% over all images
for image_counter = 1:image_n
% extract image
current_image = image( :, :, image_counter );
% first image?
if ( image_counter == 1 )
% write initial image
imwrite( current_image, color_map, strcat( "W:\image_", sprintf( "%04d", loop_counter ), ".gif" ), 'Loopcount', inf, 'DelayTime', image_delay_s );
else
% append further images
imwrite( current_image, color_map, strcat( "W:\image_", sprintf( "%04d", loop_counter ), ".gif" ), 'WriteMode', 'append', 'DelayTime', image_delay_s );
end
% image_counter
end
% increase loop counter
loop_counter = loop_counter + 1;
end
3 Comments
Thomas Bielser
on 22 Sep 2023
Thanks for your reply.
As mentioned, in my case adding a wait command in the loop (up to the range of seconds) brought only moderate improvement apart from not beeing practical if you have to write a very large number of frames. My gut feeling tells me that it is a fundamental problem in the synchronisation of the writing process of such frames.
Anton Semechko
on 4 Jul 2024
I agree with Thomas. Pausing can cause significant overhead when .gifs have >20 frames and you need to generate 100+ .gifs. But if its the only option available, I will take it.
See Also
Categories
Find more on Orange 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!