MATLAB: Index exceeds matrix dimensions error message

MATLABmatrix dimension

This is the code I'm currently working on, but when I run menu #4, I get an error saying the index exceeds matrix dimensions.
close all;
clear all;
clc;
n=2;
filename='data';
A={'Vehicle model number','Vehicle purpose','Vehicle registration number','starting year of registration','ending year of registration', 'owner'};
count=0;
while(1)
fprintf('' *programme name* \n')
fprintf('---------------------------\n')
fprintf('1) vehicle registration\n')
fprintf('4) vehicle registration statistics\n')
fprintf('0) close the programme\n')
fprintf('---------------------------\n')
m=input('Enter the choice : ');
if m==1
fprintf(' *1) vehicle registation \n')
fprintf('---------------------------\n')
number=input('- Vehicle information number :','s');
year=input('- starting year of registration(year) :');
period=input('- registration period(year) :');
owner=input('- owner :','s');
Anew={number(1:3),number(4),number(5:8),year,year+period,owner};
A(n,1)=(Anew(1,1));
A(n,2)=(Anew(1,2));
A(n,3)=(Anew(1,3));
A(n,4)=(Anew(1,4));
A(n,5)=(Anew(1,5));
A(n,6)=(Anew(1,6));
n=n+1;
end
if m==4
fprintf(' *4) vehicle registration statistics \n')
fprintf('---------------------------\n')
fprintf('Statistical conditions \n')
fprintf('1) registration period \n')
fprintf('2) purpose of registered vehicle \n')
fprintf('---------------------------\n')
choice=input('');
if choice==1
fprintf('---------------------------\n')
result=input('enter the registration period:');
fprintf('---------------------------\n')
for i=2:n
period_1=cell2mat(A(i,5));
period_2=cell2mat(A(i,4));
period_search=(period_1-period_2);
if result==period_search
count=count+1;
end
end
fprintf('---------------------------\n')
fprintf('There are %d vehicles \n', count)
fprintf('---------------------------\n')
end
end
if m==0
xlswrite('data',A);
break;
end
end
period_1=cell2mat(A(i,5)); is where the error comes up. Can somebody help me fix this problem? Thanks in advance.

Best Answer

From your code, it seems like you have n rows in your matrix, and you are trying to access row n+1. Change the limits of for-loop
for i=2:n-1
Related Question