What is wrong with my code?

14 ビュー (過去 30 日間)
Jake  Sheridan
Jake Sheridan 2022 年 11 月 11 日
編集済み: Askic V 2022 年 11 月 11 日
hammer_data16 = importdata('10_24_2022m002_a_hammer_16dat.txt');
hammer_time16 = hammer_data16(:,1);
hammer_out16 = hammer_data16(:,3);
hammer_in16 = hammer_data16(:,2);
It is telling me "Index in position 2 exceeds array bounds. Index must not exceed 1".

回答 (1 件)

Askic V
Askic V 2022 年 11 月 11 日
Execute command:
[r,c] = size(hammer_data16)
To see actual dimensions of the matrix hammer_data16 and then you'll see where the problem is. In order to diagnose further, more data is needed such as '10_24_2022m002_a_hammer_16dat.txt' file and the data line where Matlab interpreter reports the error.
But you'll probably figure it out yourself when you look at r and c variables.
  2 件のコメント
Jake  Sheridan
Jake Sheridan 2022 年 11 月 11 日
Both r and c =1, I am still confused with the problem.
Askic V
Askic V 2022 年 11 月 11 日
編集済み: Askic V 2022 年 11 月 11 日
I think you now see the problem. if c is 1 it means it has only one column (in fact since r is also 1, it is only a number).
In line
hammer_out16 = hammer_data16(:,3);
you're trying to read all elements in the third column hence index is exceeded, because there is no third column of the data.
Please attach your source data file that you read into hammer_out16, because nothing is wrong with your code, the data in the file is the most likely a problem, especially if only one numer is read from the data file.

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

カテゴリ

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