Error using movie command in Matlab

3 views (last 30 days)
Adrian
Adrian on 8 May 2014
Commented: Adrian on 9 May 2014
I generated multiple images which I converted into frames with im2frame in order to create a movie. I used this code:
for i = 10:20
images = sprintf('img%d.jpg',i);
ImageData = imread(images);
M(i) = im2frame(ImageData);
end
movie(M)
movie2avi(M,'sonar.avi','compression','None','fps',5,'quality',100)
When I run it, I get the following error:
Error using hgMovie
Movie contains uninitialized frames
Error in movie (line 41)
builtin('hgMovie',varargin{:});
Error in open83B_edited_2 (line 324)
movie(M)
Does anyone have a clue what might be wrong with my code? Thank you!

Accepted Answer

Geoff Hayes
Geoff Hayes on 8 May 2014
Hi Adrian,
I was able to reproduce the same issue with your above code. The problem is how the M array is being updated. The for loop iterates from 10 to 20 and the code uses these iteration values to assign the frame to the array. This means that M becomes an array of 20 frames, with the first nine not initialized - hence the error.
You can try the following instead:
% use this index into M
idxInM = 1;
for i = 10:20
images = sprintf('img%d.jpg',i);
ImageData = imread(images);
M(idxInM) = im2frame(ImageData);
idxInM = idxInM + 1;
end
Or any other mechanism that allows you to control how M is updated at each iteration.
  1 Comment
Adrian
Adrian on 9 May 2014
Thank you very much for your answer. In the end, I managed to solve it right away. You are right, yes. What I did is just write M(i-9) instead of M(i), and it worked. You can also skip out the index, and just write M, and still it would work.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!