Increase efficiency of gif generation and/or plotting

조회 수: 10 (최근 30일)
Eileen Lukens
Eileen Lukens 2021년 7월 29일
댓글: Jan 2021년 7월 30일
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
Eileen Lukens
Eileen Lukens 2021년 7월 30일
Thanks for all the suggestions! I will give them a try. I may be able to display fewer points and still get the same general effect. I appreciate the effort both of you put in to help me out!
Jan
Jan 2021년 7월 30일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by