MATLAB Answers

Index Exceeds the number of array elements (993268)

4 views (last 30 days)
I am trying to run my following code for a large data packet size, but when I do, I get the following error: "Index Exceeds the number of array elements (993268)" and "Index exceeds the number of array elements (0)" The error is in line 10 and 18. I am not sure what to do to resolve this issue, any help would be appreciated.
Thank you
clear all
clc
filetext = fileread('1066desk.txt');
a=length(filetext);
b=round(a/100);
iwant = cell(1,b) ;
iwant{1}=filetext(1:100);
for i = 2:b
iwant{i} = filetext((i-1)*100+1:i*100) ;
end
%% 100 to 8 division
c = cell(13,b) ;
for i=1:b
for j=1:13
if j==1
c{j,i}=iwant{i}(j:8*j);
f(j,i) = hex2dec(c(j,i)) ;
else if j==2
c{j,i}=iwant{i}(8*(j-1)+1:8*j);
f(j,i) = hex2dec(c(j,i)) ;
else if j==13
c{j,i}=iwant{i}(j*8-7:8*j-4);
f(j,i) = hex2dec(c(j,i)) ;
else if j==12
c{j,i}=iwant{i}(8*(j-1)+1:8*j);
f(j,i) = hex2dec(c(j,i)) ;
else
c{j,i}=iwant{i}(8*(j-1)+1:8*j);
f(j,i) = typecast(uint32(hex2dec(c(j,i))),'single');
end
end
end
end
end
end
g=f.*[1;1/(16*10^6);1;1;1;1;1;1;.3;.3;.3;1/4096;1];
plot(f(2,:)*1/(16*10^6),f(5,:))
title('Row 2 verses Row 5')
xlabel('Row2');
ylabel('Row5')

  2 Comments

Guillaume
Guillaume on 29 Jan 2020
I'm sorry but your code is completely unreadable.
First thing you should do is rename all the variables to something that has meaning rather than going through the alphabet. filelength is a much meaningful variable name than a. We don't have to go back to the beginning of your code to find out what it stores. Similarly, chunklength would be a much better name than b. At this point, I gave up reading your code since I can't remember what c, f, g, i, j, etc. represent.
Second thing you need to do is write comments that explain what the code is meant to do. Looks like your first loop split the file content into chunks. You don't need a loop for that.
I've no idea what the second loop tries to do. Chances are the loops are also completely unnecessary.
Taylor Knuth
Taylor Knuth on 30 Jan 2020
Thanks for your suggestions, I have gone back and cleaned the code and described the sections. If you have suggestions on how to resolve the error of "index exceeds the number of arry elements, that would be greatly appreciated.
Thanks
clear all
clc
filetext = fileread('1066desk.txt'); %import data from .txt file (data is one single string)
filelength=length(filetext); %finite length of string
numberofdivisions=round(filelength/100); %determine number of hexadecimal data packets (each data packet is 100 character string)
iwant = cell(1,numberofdivisions) ;
iwant{1}=filetext(1:100);
for i = 2:numberofdivisions
iwant{i} = filetext((i-1)*100+1:i*100) ; %divides entire string into strings of 100 characters
end
%% 100 to 8 division (Each Cell in matrix is made up of 8 characters, except for row 13 which is 4 characters)
%Simultaneously convert hexadecimal to decimal
hexmatrix = cell(13,numberofdivisions) ; %creating a [13xn] matrix for arguments and number of data packets
for i=1:numberofdivisions
for j=1:13
if j==1
hexmatrix{j,i}=iwant{i}(j:8*j);
decimalmatrix(j,i) = hex2dec(hexmatrix(j,i)) ; %row 1 is converted to decimal
else if j==2 %row 2 is converted to decimal with 8 characters
hexmatrix{j,i}=iwant{i}(8*(j-1)+1:8*j);
decimalmatrix(j,i) = hex2dec(hexmatrix(j,i)) ;
else if j==13 %13th row only with 4 character and converted to decimal
hexmatrix{j,i}=iwant{i}(j*8-7:8*j-4);
decimalmatrix(j,i) = hex2dec(hexmatrix(j,i)) ;
else if j==12 %12th row converted to decimal with 8 characters
hexmatrix{j,i}=iwant{i}(8*(j-1)+1:8*j);
decimalmatrix(j,i) = hex2dec(hexmatrix(j,i)) ;
else
hexmatrix{j,i}=iwant{i}(8*(j-1)+1:8*j); %convert rows 3-11 to float 4
decimalmatrix(j,i) = typecast(uint32(hex2dec(hexmatrix(j,i))),'single');
end
end
end
end
end
end
%% Scalar Conversions and Visualizations
ConversionMatrix=decimalmatrix.*[1;1/(16*10^6);1;1;1;1;1;1;.3;.3;.3;1/4096;1];
plot(ConversionMatrix(2,:),ConversionMatrix(5,:)) %plot rows versus rows
title('Time verses Acceleration in X')
xlabel('Time (s)');
ylabel('Acceleration in X (m/s^2)')

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 29 Jan 2020
If you have an array with 10 elements but you are trying to access the 11th element, you got this error. Just step through your code and verify the numbers.
a=ones(10,1)
a(11)

  3 Comments

Taylor Knuth
Taylor Knuth on 29 Jan 2020
I am unsure of what you mean, can you please explain?
Fangjun Jiang
Fangjun Jiang on 29 Jan 2020
In your code and example, variable "filetext" has 993268 elements, thus a is 993268, b is 9933 due to round() operation. In the FOR loop, when "i" is b which is 9933, you try to get filetext(993300) which exceeds the maximum number of elements in "filetext".
Maybe the cause of the problem is the use of round(). Use floor() instead.
Taylor Knuth
Taylor Knuth on 30 Jan 2020
Thanks, that fixed the error. Thanks for your time!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2018b

Translated by