Index exceeds matrix dimensions error message

1 回表示 (過去 30 日間)
Yoobin Lee
Yoobin Lee 2020 年 5 月 19 日
コメント済み: Ameer Hamza 2020 年 5 月 19 日
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.

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 5 月 19 日
編集済み: Ameer Hamza 2020 年 5 月 19 日
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 件のコメント
Yoobin Lee
Yoobin Lee 2020 年 5 月 19 日
Thanks, it worked! :D
Ameer Hamza
Ameer Hamza 2020 年 5 月 19 日
I am glad to be of help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by