What does "Index exceeds the number of array elements (6)." mean?

35 ビュー (過去 30 日間)
Jonathan Babitsch
Jonathan Babitsch 2020 年 6 月 16 日
コメント済み: Jonathan Babitsch 2020 年 6 月 16 日
Hey everybody,
I have a problem concerning my recent work. I get the following error message:
I also attached the code if that helps.
I really have no idea what it is from and why it occures here... What can I do here to avoid the failure?
Thank you a lot in advance!!
Johnny
  1 件のコメント
KSSV
KSSV 2020 年 6 月 16 日
xlfile is missing......code is huge...
Error is simple, you are trying to extract more number of elemnts then present.
Ex:
A = rand(6,1) ;
A(8)

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

採用された回答

Mehmed Saad
Mehmed Saad 2020 年 6 月 16 日
編集済み: Mehmed Saad 2020 年 6 月 16 日
You are using allepunkte as a for loop iterator. From Line 197 to Line 222 and you basically index two variables namely inside and outside in if else condition. The code syntax is
for allepunkte 1:8
for huellkoerper 1:4
if First_Condition
inside(allepunkte) = true;
else
outside(allepunkte) = true;
end
end
end
Now the problem is you have not initialized the variable inside and outside which means that the maximum index they will get will be their size. So initialize them before for loop starts. This will resolve the index problem
  1 件のコメント
Jonathan Babitsch
Jonathan Babitsch 2020 年 6 月 16 日
I see this makes perfectly sense! So that means inside+outside is the size of allepunkte? Means in the code I can also put inside{allepunkte}=true and in the same condition outside{allepunkte}=false, then I have a value assigned to every index... This seems to work... Thank you a lot for your help Mehmed, have a nice day!!

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

その他の回答 (1 件)

Deepak Gupta
Deepak Gupta 2020 年 6 月 16 日
Hello Jonathan,
It's hard to root cause the problem without having the xlsx sheet you are using in your program. But in general this error is thrown when you are trying to access an array index which doesn't exist. At line number 229, you are checking condition, for allepunkte = 1:8, and for that it throws error that index exceeds array size of 6. So program will throw error for any value greater than 6. Try allepunkte = 1:6 at this point and check if you still see error. It's not the solution, it's just a way to root cause the error.
If this doesn't throw error and you have to keep allepunkte = 1:8, then you need to investivate why you are not getting cell arrays of required size.
Hope this helps.
  1 件のコメント
Jonathan Babitsch
Jonathan Babitsch 2020 年 6 月 16 日
Thank you Deepak!
You described it nicely and it really led me the right way.
Have a nice day and as I said thanks a lot!

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by