What is wrong with my code?

14 vues (au cours des 30 derniers jours)
Jake  Sheridan
Jake Sheridan le 11 Nov 2022
Modifié(e) : Askic V le 11 Nov 2022
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".

Réponses (1)

Askic V
Askic V le 11 Nov 2022
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 commentaires
Jake  Sheridan
Jake Sheridan le 11 Nov 2022
Both r and c =1, I am still confused with the problem.
Askic V
Askic V le 11 Nov 2022
Modifié(e) : Askic V le 11 Nov 2022
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by