필터 지우기
필터 지우기

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 CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by