preallocation

hey guys, i am new at MATLAB, I have problem in preallocating the space for the variable'M' in the following statement:
for i=1:10000
M(i-1)=im2frame(int8(fg),gray(256));
I don't know what is the diminsion of this structure or the statement for preallocating space for M

 Accepted Answer

Walter Roberson
Walter Roberson on 3 Jun 2012
A "frame" is a structure with the fields "cdata" and "colormap"
You should be able to preallocate with
M(10000) = struct('cdata', {[]}, 'colormap', {[]} );
Caution: in your code, you try to store into M(i-1) when "i" starts at 1. That is going to try to store into M(0) which is not a valid index for MATLAB arrays: indices must be at least 1

4 Comments

Duke Watson
Duke Watson on 3 Jun 2012
when i am applying this ,it will generate the error for the below statement:
movie2avi(M,'frame_difference','fps',30);
error is:
the frame must be 384 by 288. how can i overcome this problem?
Walter Roberson
Walter Roberson on 3 Jun 2012
You need to ensure that your frames are 384 x 288 (I don't know why that particular size.)
You might need to crop some of your frames, or you might need to imresize some of your frames.
As best I can tell, the 384 x 288 is not a preset value (that is, larger frames can be used), but it might represent the size of the first frame or perhaps of the smallest frames.
In particular it is an error to use frames of different sizes in your movie structure. Different sizes can crop up if you use getsnap() or getframe() if the axes moves or if the axes resizes.
Duke Watson
Duke Watson on 3 Jun 2012
Sorry,i didn't get you.
I think all the frames all of the same size as i have extracted frames from a video and convert them into gray image and then again convert them into frames and then finally into video.(Frame Difference Algorithm).
Now if you get me,please tell me what should i do?
Walter Roberson
Walter Roberson on 3 Jun 2012
T1 = arrayfun( @(S) size(S.cdata), M, 'Uniform', 0);
T2 = cell2mat(T1(:));
T3 = unique(T2, 'rows');
T4 = size(T3,1);
fprintf(1, 'You have %d different sizes of frames\n', T4);

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!