How to make frame size same for subplots run in loop to make video using videowriter?

10 views (last 30 days)
Hi friends,
I have written a code to make movie using videowriter command. But I am getting error.
close all
clear all
clc
theta=0:0.1:2*pi;
vidObj = VideoWriter('testMovie.avi');
open(vidObj);
for ii=1:100
ii
subplot(1,2,1)
plot(theta,sin(theta));
subplot(1,2,2)
plot(theta,cos(theta));
currFrame=getframe;
writeVideo(vidObj,currFrame);
close all
end
close(vidObj);
The error is saying frame is not matching with the previous figure.
Thanks in advance.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Feb 2019
yes that can happen . plot() inside an axes can end up rendered slightly different sizes.
probably the easiest way to fix this is to imresize the result of getframe to a consistent size.
It is possible that you might notice the axes positions shifting slightly from frame to frame and it could even be the case that what should be the same curves move slightly depending upon the labels associated with the data. If you need the boxes to be exactly aligned frame to frame then you might need further precautions .
  4 Comments
Raj Kishor
Raj Kishor on 25 Feb 2019
Thanks walter
It worked after small modification.
if rr == 1
framesize = [size(currFrame.cdata, 1), size(currFrame.cdata,2 )];
else
currFrame = imresize(currFrame.cdata, framesize );
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!