MATLAB Answers

Error in alternateGetframe when creating a video in a large for loop

38 views (last 30 days)
Meva
Meva on 11 Apr 2016
Answered: sam0037 on 14 Apr 2016
Hello,
I am using Matlab 2015a, 2015b.
I think there is limitation of matlab for creating movies!
I tried all possibilities and it still does give alternateGetframe error after 152nd frame. So, it did not create a movie for large nFrame.
Here it is;
.....
writerObj = VideoWriter('Body.avi');
open(writerObj);
%# preallocate
nFrame=10000;
mov(1:nFrame) = struct('cdata',[], 'colormap',[]);
clear mov
for nt=1:nFrame;
............
hfig = figure('color','w','un','pix','pos',[360 750 450 400])
%
X=[xx,fliplr(xx)];
Y=[body_down,fliplr(body_up)];
C=[baseLine,fliplr(constriction)];
b=fill(X,Y,[0.2 0.4 0.7]); %#plot filled area
hold on
plot(xx(index),baseLine.*ones(size(index)),'k','Linewidth',2);
hold on
plot(xx(index),upperLine.*ones(size(index)),'k','Linewidth',3);
hold on
s=fill(xx(index([1 1:end end])),...
[baseLine constriction(index) baseLine],...
[0 0.3 0],'EdgeColor','k');
xlabel('x')
set(s, 'FaceAlpha', 0.5)
set(b, 'FaceAlpha', 0.25)
% x3 = 0.6;
% y3 = -0.2;
% str3 = ' Dilation, \alpha = 1.5 ';
% text(x3,y3,str3)
%-----------------------
% x2 = 0.47;
% y2 = 0.8;
% str2 = 'Body';
% text(x2,y2,str2)
axis([0 1 -0.4 1.05]);
set(gcf,'Color',[1 1 1])
box on
set(gca,'XMinorTick','on','YMinorTick','on')
title(['At t = ',num2str(t)])
box on
mov(nt) = getframe(gcf);
writeVideo(writerObj,mov(nt));
end
close(writerObj)

  0 Comments

Sign in to comment.

Answers (1)

sam0037
sam0037 on 14 Apr 2016
Hi,
I was able to create a video using nFrames = 10000 in MATLAB R2015b. I did make a couple of changes to the above code. I will illustrate the example using a plot of sine curve from 0 to 2pi rather than the plot used in the question above (I leave that to you for further modifications). However, I made a couple of changes to the code:
1. Instead of creating the figure each time inside the loop, declare it once outside the loop. It avoids creating unnecessary figure handles which in turn could exhaust the memory.
2. If the 'mov' struct array is being not used outside the loop, consider using a temporary variable inside the loop. The 'mov' struct array will also consume a lot of memory depending on the number of frames used. (In my case it works using both a temporary variable and a struct array).
writerObj = VideoWriter('SineWave.avi');
open(writerObj);
nFrame=10000;
hfig = figure('color','w','un','pix','pos',[360 550 450 400]);
axis([0 2*pi -1.5 1.5])
hold on;
time = linspace(0,2*pi,nFrame);
for nt=1:nFrame
plot(time(nt),sin(time(nt)),'marker','*');
% mov(nt) = getframe(gcf);
% writeVideo(writerObj,mov(nt));
mov = getframe(gcf);
writeVideo(writerObj,mov);
end
close(writerObj)
Hope making these changes helps. If this still does'nt work, then share the error message you receive. This would help in troubleshooting the issue.

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by