Index exceeds matrix dimensions error message

1 vue (au cours des 30 derniers jours)
Yoobin Lee
Yoobin Lee le 19 Mai 2020
Commenté : Ameer Hamza le 19 Mai 2020
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.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 19 Mai 2020
Modifié(e) : Ameer Hamza le 19 Mai 2020
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
  2 commentaires
Yoobin Lee
Yoobin Lee le 19 Mai 2020
Thanks, it worked! :D
Ameer Hamza
Ameer Hamza le 19 Mai 2020
I am glad to be of help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur NaNs dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by