MATLAB: Using pause() and readframe() – the pause is not affecting the output

readframe

I am trying to pause the video and graphs when the graphs reach a maximum. The code successfully pauses in the matlab figure window but, when I go back and watch the video 'compositevid2' in the default windows player, the video does not slow down at all. I've tried increasing the the pause time to two minutes and haven't noticed any change to the outputed video. Is it possible to do what I am wanting to do?
%open the video
inputVid = VideoReader('760-255-20 Test B04 - 3000 fps.wmv');
%calculate total number of frames, mostly to show you what data is
%available in the object.
TotnumFrames = round(inputVid.FrameRate*inputVid.Duration);
%%now add stats to it.
%initiate new composite video file
mergedobj = VideoWriter('compositevid2','Motion JPEG AVI');
mergedobj.FrameRate = inputVid.FrameRate; %match same framerate
mergedobj.Quality=100;
open(mergedobj);
%start the stitch
hfig = figure;
k = 1; %use for my data
[t, p4, p11, dps] = grabfromexcel(); %pull the data from excel
[maxp4Value, indexAtMaxp4]=max(p4);
tValueAtMaxp4Value = t(indexAtMaxp4(1));
[maxp11Value, indexAtMaxp11]=max(p11);
tValueAtMaxp11Value = t(indexAtMaxp11(1));
%while loop until there are no more frames
n=30;
m=30;
while k <= (length(t)/dps)
%read in frame
singleFrame = readFrame(inputVid);
% display frame
gaugeposition=[700, 600, 700, 700; 1400, 600, 1400, 700];
textposition=[700,700; 1400, 700];
labelgauges=cell(2,1);
gaugenumbers = [4, 11];
for ii=1:2
labelgauges{ii}=['Gauge ' num2str(gaugenumbers(ii), '%.0f')];
end
RGB1=insertShape(singleFrame,'line',gaugeposition, 'Color', 'red', 'LineWidth',3);
RGB2=insertText(RGB1, textposition, labelgauges,'FontSize', 40, ...
'BoxColor', 'white', 'BoxOpacity', .3, 'TextColor', 'red', 'AnchorPoint', 'CenterTop');
subplot(4,4,1:12),imagesc(RGB2), axis off, axis equal;
if any(t(1,((k-1)*dps)+(1:dps)) == tValueAtMaxp4Value)
text(tValueAtMaxp4Value, max(p4), commentp4, 'HorizontalAlignment', 'right')
pause on
pause (40)
pause off
end
if any(t(1,((k-1)*dps)+(1:dps)) == tValueAtMaxp11Value)
text(tValueAtMaxp11Value, max(p11), commentp11, 'HorizontalAlignment', 'left')
pause on
pause (40)
pause off
end
%plot the data
subplot(4,4,13:14),plot(t(1,((k-1)*dps)+(1:dps)),p4(1,((k-1)*dps)+(1:dps)),'k-.'),hold on;
axis on
title('Pressure Gauge 4 vs Time');
ylim([-1 (max(p4)+.25)])
xlim([0 max(t)])
commentp4= ['Max Pressure= ', num2str(maxp4Value), ' \rightarrow '];
if any(t(1,((k-1)*dps)+(1:dps)) == tValueAtMaxp4Value)
text(tValueAtMaxp4Value, max(p4), commentp4, 'HorizontalAlignment', 'right')
pause on
pause (40)
pause off
end
subplot(4,4,15:16),plot(t(1,((k-1)*dps)+(1:dps)),p11(1,((k-1)*dps)+(1:dps)),'k-.'),hold on;
axis on
title('Pressure Gauge 11 vs Time');
ylim([-1 (max(p4)+.25)])
xlim([0 max(t)])
commentp11= [' \leftarrow Max Pressure= ', num2str(maxp11Value)];
if any(t(1,((k-1)*dps)+(1:dps)) == tValueAtMaxp11Value)
text(tValueAtMaxp11Value, max(p11), commentp11, 'HorizontalAlignment', 'left')
pause on
pause (40)
pause off
end
set(gcf, 'WindowState', 'maximized');
%grab what the figure looks like
frame = getframe(hfig);
%write to file.
writeVideo(mergedobj, frame);
%increment k for my data.
k = k+1;
end
%close the object
close(mergedobj)

Best Answer

getframe and writeVideo merely store images as frames of a movie which does not affect playback speed.
Follow Walter Roberson's advice in this answer: