Borrar filtros
Borrar filtros

Index exceeds the number of array elements .Please help??

1 visualización (últimos 30 días)
Kadir Tolga Dogukanli
Kadir Tolga Dogukanli el 22 de En. de 2019
Comentada: Jan el 22 de En. de 2019
I have a code to plot mpu6050 data , but when i run it ,i have errors :
Index exceeds the number of array elements (33).
Error in dene (line 23)
addpoints(veri1,x(k),double(ax(k)));
please help
clear all;
close all;
com=serial('COM3','Baudrate',38400); %you will have different arguments
fopen(com)
veri1 = animatedline('Color',[1 0 0]); %x,y ve z eksenlerinden alınan verileri çizdiren,rengini belirlediğimiz animasyon çizgileri tanımlıyoruz.
veri2 = animatedline('Color',[0 1 0]);
veri3 = animatedline('Color',[0 0 1]);
veri1.MaximumNumPoints = 10000;
veri2.MaximumNumPoints = 10000;
veri3.MaximumNumPoints = 10000;
n=100;
acx=zeros(1,n);
acy=acx;
acz=acx;
x = linspace(0,10000)
for k=1:10000
ax=fscanf(com,'%d');
ay=fscanf(com,'%d');
az=fscanf(com,'%d');
addpoints(veri1,x(k),double(ax(k)));
addpoints(veri2,x(k),double(ay(k)));
addpoints(veri3,x(k),double(az(k)));
pause(0.1);
drawnow limitrate
end
fclose(com)
  3 comentarios
Kadir Tolga Dogukanli
Kadir Tolga Dogukanli el 22 de En. de 2019
can you help, now?
Warning: Matching failure in format. The format
specified for conversion, '%d', is incorrect.
Index exceeds the number of array elements (37).
Error in dene (line 23)
addpoints(veri1,x(k),double(ax(k)));
Jan
Jan el 22 de En. de 2019
Start with solving the warning: The '%d' in fscanf seems to be a problem. Why?
Afterwards the error message sound line ax has 37 elements, but k is 38.
As I have written in my answer, I assume that this is not wanted: You import some data by ax=fscanf(com,'%d'), but access the k.th element in ax(k). You cannot be sure that the COM interface provides as many inputs as the value of the loop counter is currently.
I cannot guess, what you want to do instead. I recommend to start with a smaller code, which imports the data from the COM interface and display it by a simple disp() command. Even if this runs reliably, add further details like an animated line.
The code needs more comments which describe the purpose of each line. If the reader of a code has the code only, a debugging is impossible, because the failing code is the only available information.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 22 de En. de 2019
Editada: Jan el 22 de En. de 2019
x = linspace(0,10000)
This creates 100 elements netwee 0 and 10000. Then x(k) will fail at k=101.
The debugger allows for finding such problems very fast. I recommend to use it.
Maybe you mean:
x = linspace(0, 10000, 10000);
But remember, that this is not [0, 1, 2, ...]
Perhaps it is easier to use k directly instead of x(k), if you want [1,2,3,...]
By the way, this looks strange:
ax=fscanf(com,'%d');
...
... double(ax(k)) ...
Are you sure, that you import a vector ax in each iteration, and use its 1st element in the first iteration and the 10'000th element in the last iteration?
  2 comentarios
Kadir Tolga Dogukanli
Kadir Tolga Dogukanli el 22 de En. de 2019
I am new on matlab . I write this code , through errors . if you have any idea about it can you help me?
Jan
Jan el 22 de En. de 2019
Before I can help you reliably, it is required, that you explain, what the code should do. It is hard to fix some code without knowing its purpose. Therefore good codes contain exhaustive comments.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by