Index exceeds the number of array elements .Please help??
5 views (last 30 days)
Show older comments
Kadir Tolga Dogukanli on 22 Jan 2019
Commented: Jan on 22 Jan 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)
com=serial('COM3','Baudrate',38400); %you will have different arguments
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;
x = linspace(0,10000)
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.
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:
... 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?
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.
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!