how to fix Index exceeds matrix dimensions in Matlab?

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

Can you tell us what is size of A?

i am a newbie and i use codes from "https://angeljohnsy.blogspot.com/2012/03/how-to-add-captionsubtitles-to-video-in.html"

this is info of video info =
Duration: 3.2280
Name: 'Poem2.avi'
Path: 'C:\Users\Nguye...'
Tag: ''
Type: 'VideoReader'
UserData: []
BitsPerPixel: 24
FrameRate: 6.0000
Height: 480
NumberOfFrames: 18
VideoFormat: 'RGB24'
Width: 640

Sign in to comment.

Answers (1)

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

Could you tell me specifically? This is my first time i use Matlab

Sign in to comment.

Asked:

on 21 Nov 2017

Edited:

on 21 Nov 2017

Community Treasure Hunt

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

Start Hunting!