MATLAB: Figure gets displayed only after terminating the program. What to do

displayfigurevideo

im using subplots with imshow in my program which reads frames from a video file. Output figure is not getting displayed. If i terminate the program using Ctrl+C, the figure shows up. However instead of video file if I read input from webcam with videoinput, I get the output figure normally as usual. Code:
foregroundDetector = vision.ForegroundDetector('NumGaussians',3,'NumTrainingFrames',10,'LearningRate',0.000001);
foregroundDetector1 = vision.ForegroundDetector('NumGaussians',3,'NumTrainingFrames',10,'LearningRate',0.000001);
%vid = videoinput('winvideo', 1); %select input device
vid = vision.VideoFileReader('daria_walk.avi');
i=1;
DlgH = figure;
H = uicontrol('Style', 'PushButton', ...
'String', 'Close', ...
'Callback', 'delete(gcbf)');
while (ishandle(H))
frame = step(vid);
%frame = getsnapshot(vid);
YCBCR = rgb2ycbcr(frame);
rgbForeground = step(foregroundDetector, frame);
ycbcrForeground = step(foregroundDetector1, YCBCR(:,:,3));
se = strel('square', 3);
filteredForeground = imopen(rgbForeground, se);
subplot(1,2,1);
imshow(filteredForeground);
title('RGB Foreground');
subplot(1,2,2);
imshow(ycbcrForeground);
title('YCBCR Foreground');
end
close

Best Answer

Try to insert a drawnow command to instruct Matlab to update the display.