MATLAB Answers

Increase efficiency of gif generation and/or plotting

4 views (last 30 days)
Eileen Lukens
Eileen Lukens on 29 Jul 2021
Commented: Jan on 30 Jul 2021
I have a gif that I want to make but when I run my script, it takes upwards of 15 minutes to generate the gif file and uses a ton of memory on my computer. The data set I am using is farily large. I supressed the figure window to speed things up as well.
For context, I have tried removing everything from the loop and only replacing the 'Zdata' but that doesn't speed things up. Is there a way to preallocate frames or do something to speed up the code and make it use less memory? Is there a particular line of code that is slowing things down that I could change?
I also get this as an occasional error
% Warning: MATLAB previously crashed due to a low-level graphics error while
% using software OpenGL. For more information, see Resolving Low-Level Graphics
% Issues.
% > In matlab.graphics.internal.initialize (line 15)
I've included an example of how my code is set up below. Note that the loop only has four iteration but it still takes quite a long time. my matlab program also crashes when I do more than four loops. I want to do upwards of 100 interations.
Thanks!
%%%% example code
%clearing
clear
clc
close all
%generate a random data set
time = 1:1:100;
for i = 1:length(time)
z{i} = rand(3600,2020); %this is the same size as my dataset
end
x = 1:1:size(z{1},2);
y = 1:1:size(z{1},1);
f1 = figure('visible','off');
axis tight manual
filename = 'mygiftest.gif'
%% begin loop
for i = 1:4
%%% plotting
data = z{i};
subplot(2,1,1)
h1 = surf(x(1,1700:end),y',data(:,1700:end));
hold on
b1 = surf(x(1,1:1600),y',data(:,1:1600));
xlabel('x')
ylabel('y')
zlabel('z')
colorbar('visible','off')
set(h1,'LineStyle','none')
set(b1,'LineStyle','none')
view(3)
p = get(gca,'position');
set(gca,'FontName','Arial','FontSize',16,'position',[p(1) p(2) p(3) p(4)])
subplot(2,1,2)
h2= surf(x(1,1700:end),y',data(:,1700:end));
hold on
b2 = surf(x(1,1:1600),y',data(:,1:1600));
xlabel('x')
ylabel('y')
zlabel('z')
cc = colorbar('location','southoutside');
set(cc,'position',[0.13 0.06 0.775 0.03])
cc.Label
cc.Label.String = ['z'];
cc.Label.FontSize = 16;
set(h2,'LineStyle','none')
set(b2,'LineStyle','none')
view(2)
p = get(gca,'position');
set(gca,'FontName','Arial','FontSize',16,'position',[p(1) p(2)+0.06 p(3) p(4)])
set(gca,'FontName','Arial','FontSize',16)
set(gcf,'position',[2 42 600 924],'visible','off')
sgtitle(datestr(time(i)),'FontName','Arial','Fontsize',18)
hold off
drawnow
%%% write gif
frame = getframe(f1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if i == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','DelayTime',0.05,'WriteMode','append');
end
clf(f1,'reset')
end
  6 Comments
Jan
Jan on 30 Jul 2021
I tried it with a similar approach as Dave B. The time for the first frame takes 18 sec on my i5 mobile, Matlab R2018b, and 12 sec for the following frames.
I've tried to set some of the drawing modes to 'manual', to avoid letting Matlab check the dimensions in each iteration:
ax1 = subplot(2,1,1, 'NextPlot', 'add'); % Includes the "hold on"
...
ax2 = subplot(2,1,2, 'NextPlot', 'add');
...
set(ax1, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual', ...
'XTickMode', 'manual', 'YTickMode', 'manual', 'ZTickMode', 'manual');
set(ax2, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual', ...
'XTickMode', 'manual', 'YTickMode', 'manual', 'ZTickMode', 'manual');
This has no large effects.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!