Index Exceeds the number of array elements (993268)
1 view (last 30 days)
Show older comments
Taylor Knuth
on 29 Jan 2020
Commented: Taylor Knuth
on 30 Jan 2020
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
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.
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Logical 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!