How can I fix graph title jittering in MATLAB movie.

6 views (last 30 days)
I have been trying to make a movie using MATLAB out of graphs consisting of a a pcolor graph with a quiver plot overlayed on it. The problem I am having is that the graph title jumps up and down while the movie is being displayed. Fro example when the frame jumps from 11 to 12 the title goes downward. Is there a way to fix this problem.
clear all;
close all;
writeMovie = true;
if(writeMovie)
vidObj = VideoWriter('testquiver.avi');
vidObj.Quality = 25;
vidObj.FrameRate = 10;
open(vidObj);
else
end
figure
t = 0:0.1:4;
[X,Y] = meshgrid(-10:.2:10,-2:.2:2);
for ii = 1:length(t)
Z = X.*exp(-X*sin(t(ii)).^2 - Y.^2);
[DX,DY] = gradient(Z,.2,.2);
subplot(2,1,1);
pcolor(X,Y,Z)
shading interp
hold on
quiver(X(2:2:end),Y(2:2:end),DX(2:2:end),DY(2:2:end),'LineWidth',1,'Color', 'k')
colormap hsv
hold off
xlabel('some text');
ylabel('some text');
title(['some more text frame number = ', num2str(ii)]);
axis image
axis off
subplot(2,1,2)
pcolor(X,Y,Z*-1)
[DX,DY] = gradient(Z*-1,.2,.2);
shading interp
hold on
quiver(X(2:2:end),Y(2:2:end),DX(2:2:end),DY(2:2:end))
colormap hsv
hold off
pause(0.1);
if(writeMovie)
currFrame = getframe(gcf);
writeVideo(vidObj,currFrame);
else
end
end
% Create AVI file.
if(writeMovie)
close(vidObj);
end
  3 Comments
Timothy
Timothy on 16 Aug 2012
Yes. It should play a movie with 41 total frames. I just copied and pasted the code above into MATLAB and it worked for me.
Matt Fig
Matt Fig on 16 Aug 2012
Yes, I see the plot itself changing, but if I open the .avi file with my default movie player, it contains only one frame that keeps looping - no change.
Must be a bug on my machine or in MATLAB.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 16 Aug 2012
Edited: Image Analyst on 16 Aug 2012
I agree with Matt. The movie looks like garbage. Mine is some kind of blurred frame #1 blended with MATLAB code in the background and that frame never changes throughout the whole movie. Not sure why since it looks like you followed the directions in the help. Maybe you could use movie2avi instead.
Anyway, the title moving is due to the quiver arrows sticking out of the image. When they extend out past the colored part of your frame, the title moves up to maintain a constant distance between the title and the top-most part of the chart/image. I don't know how to fix. You might try playing with the 'Position' property of the image or quiver. And there are several flavors of position to look at, like 'outerposition', etc.
Alternatively you might want to look into avoiding pcolor (which I hate) at all. Try to do the same thing with image() or imshow().

Matt Fig
Matt Fig on 17 Aug 2012
Try this out. It stops the jittering on my machine. Still don't know about the .avi file, but the plotting is steady. I only added a few lines of code to what you wrote.
clear all;
close all;
writeMovie = true;
if(writeMovie)
vidObj = VideoWriter('testquiver.avi');
vidObj.Quality = 25;
vidObj.FrameRate = 10;
open(vidObj);
else
end
figure('pos',[624 474 672 504])
t = 0:0.1:4;
[X,Y] = meshgrid(-10:.2:10,-2:.2:2);
for ii = 1:length(t)
Z = X.*exp(-X*sin(t(ii)).^2 - Y.^2);
[DX,DY] = gradient(Z,.2,.2);
subplot(2,1,1);
P = pcolor(X,Y,Z);
shading interp
hold on
Q = quiver(X(2:2:end),Y(2:2:end),DX(2:2:end),DY(2:2:end),'LineWidth',1,'Color', 'k');
colormap hsv
% hold off
xlabel('some text');
ylabel('some text');
axis image
axis off
subplot(2,1,2)
P(2) = pcolor(X,Y,Z*-1);
[DX,DY] = gradient(Z*-1,.2,.2);
shading interp
hold on
Q(2) = quiver(X(2:2:end),Y(2:2:end),DX(2:2:end),DY(2:2:end));
colormap hsv
axis off
if ii==1
T = text(-7,8,'temp','fontweight','bold','fontsize',14);
end
if ii<10
STR = ['some more text frame number = 0', num2str(ii)];
else
STR = ['some more text frame number = ', num2str(ii)];
end
set(T,'string',STR)
pause(0.1);
if(writeMovie)
currFrame = getframe(gcf);
writeVideo(vidObj,currFrame);
else
end
delete([P Q])
end
% Create AVI file.
if(writeMovie)
close(vidObj);
end

Community Treasure Hunt

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

Start Hunting!