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

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 Comments

Please post the complete error message, such that the readers do not have to guess, where the problem occurs.
Then darn clear all; is a waste of time. Use functions to keep the workspace clean, instead of removing all functions from the RAM and deleting all persistent data.
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)));
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.

Sign in to comment.

Answers (1)

Jan
Jan on 22 Jan 2019
Edited: Jan on 22 Jan 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 Comments

I am new on matlab . I write this code , through errors . if you have any idea about it can you help me?
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.

Sign in to comment.

Asked:

on 22 Jan 2019

Commented:

Jan
on 22 Jan 2019

Community Treasure Hunt

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

Start Hunting!