how to fix Index exceeds matrix dimensions in Matlab?
Show older comments
Here is my code
%Input a Video file
obj=VideoReader('Poem2.avi');
A=read(obj);
j=1;
%Preallocate the frame structure
Frame=struct('cdata',1,'colormap',cell([1 100]));
%Create a cell array with the Poem versus
Txt={'TWO roads diverged in a yellow wood,';
'And sorry I could not travel both';
'And be one traveler, long I stood';
'And looked down one as far as I could';
'To where it bent in the undergrowth;';
'Then took the other, as just as fair,';
'And having perhaps the better claim,';
'Because it was grassy and wanted wear;';
'Though as for that the passing there';
'Had worn them really about the same,';
'And both that morning equally lay';
'In leaves no step had trodden black.';
'Oh, I kept the first for another day!';
'Yet knowing how way leads on to way,';
'I doubted if I should ever come back.';
'I shall be telling this with a sigh';
'Somewhere ages and ages hence:';
'Two roads diverged in a wood, and I—';
'I took the one less traveled by,';
'And that has made all the difference.';};
%display the image
%Add the poem lines
%Grab the frame
%There are 20 lines totally
%Add 3 and 2 lines alternatively in each frame
inc=0;
for i=1:size(A,4)%size(A,1) is the number of rows of A and size(A,2) is the number of columns of A.
n=mod(i,2);
I=A(:,:,:,i);
imshow(I), hold on
for k=1:2+n
text(60,330+(k*30),Txt{k+inc},'Color','w','FontWeight','Bold','FontSize',15);
end
inc=inc+1+n;
Frame(j:j+9)=getframe;
j=j+10;
end
obj=VideoWriter('Road Not Taken.avi');
obj.FrameRate = 10;
open(obj);
writeVideo(obj,Frame);
close(obj);
When i run it, there is an error
Index exceeds matrix dimensions.
Error in SubVideo (line 46)
text(60,330+(k*30),Txt{k+inc},'Color','w','FontWeight','Bold','FontSize',15);
Please help me fix this error
3 Comments
KSSV
on 21 Nov 2017
Can you tell us what is size of A?
huy nguyen
on 21 Nov 2017
huy nguyen
on 21 Nov 2017
Answers (1)
KSSV
on 21 Nov 2017
0 votes
Error is due to: size of your Txt is 20X1....and in the loop you are trying to access Txt{21}.....you need to add more Txt....or access it till 20.
1 Comment
huy nguyen
on 21 Nov 2017
Categories
Find more on Matrix Indexing 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!