Index exceeds number of array elements error

Hello there, I'm trying to get user input data and put them into existing excel data. However, when I run the code, at line 20, said error message pops up. I don't get why it happens - can anybody help me fix this problem?
close all;
clear all;
clc;
[num,txt,everything]=xlsread('filename.xlsx');
n=length(everything)+1;
while(1)
fprintf('---------------------------\n')
fprintf('1) car registration \n')
fprintf('0) close the programme \n')
fprintf('---------------------------\n')
m=input('enter the choice : ');
if m==1
fprintf(' *1) car registration \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');
l=cell2mat(length(number)); %here I get the error
Anew={number(1:l-5),number(l-4),number(l-3:l),year,year+period,owner};
everything(n,1)=(Anew(1,1));
everything(n,2)=(Anew(1,2));
everything(n,3)=(Anew(1,3));
everything(n,4)=(Anew(1,4));
everything(n,5)=(Anew(1,5));
everything(n,6)=(Anew(1,6));
n=n+1;
end
if m==0
xlswrite('filename',everything);
break;
end
end

9 件のコメント

Rik
Rik 2020 年 5 月 22 日
The output of the length function is not a cell array. What is your goal with that line?
Yoobin Lee
Yoobin Lee 2020 年 5 月 22 日
I'm trying to get a user input and separate it into different cells. Because the input size can be different, I used 'l' to get the total length of the input. And when I tried with l=length(number) it didn't work either - it had the same problem.
Rik
Rik 2020 年 5 月 22 日
Since number is a char array, I don't see how length(number) would result in an error. What is the exact error message when you use that?
Yoobin Lee
Yoobin Lee 2020 年 5 月 22 日
編集済み: Yoobin Lee 2020 年 5 月 22 日
index exceeds the number of array elements (1)
Rik
Rik 2020 年 5 月 22 日
What does this return?
clc, which length -all
Yoobin Lee
Yoobin Lee 2020 年 5 月 22 日
Same error message pops up
Rik
Rik 2020 年 5 月 22 日
If you run this
clc, which length -all
you get "index exceeds the number of array elements (1)"?
Yoobin Lee
Yoobin Lee 2020 年 5 月 22 日
Yeah..You mean when l=num2cell(length(number)) right..?
Rik
Rik 2020 年 5 月 22 日
No, I don't. You never mentioned using num2cell, so why would I assume you did? It looks to me like you need l=length(number). You said that returns an error. What exact error are you getting for that exact line of code.
If you change the code you're running without mentioning that, it is very difficult to help you. I can't see your screen. So now the previous 4 comments don't serve any purpose.

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

回答 (1 件)

Raunak Gupta
Raunak Gupta 2020 年 5 月 26 日

0 投票

Hi,
As suggested by Rik in comments and tried by me length of the input string must not return any error statement. So cell2mat in the code can be removed because length will return a number not a cell array. Also, from the error message I can see that the number string that has been given as input must be empty. Try checking that number is correctly entered.
A small note is you are writing some lines using fprintf but I don’t see to which text file the lines are written.

2 件のコメント

Rik
Rik 2020 年 5 月 26 日
fprintf is also often used to write to the command window. As the documentation describes it is equivalent to fid=1;.
Raunak Gupta
Raunak Gupta 2020 年 5 月 27 日
Thanks for clarifying Rik.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2020 年 5 月 22 日

コメント済み:

2020 年 5 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by