Index out of bounds error very unusual

2 views (last 30 days)
Nicola
Nicola on 7 Feb 2014
Commented: Walter Roberson on 12 Aug 2015
Good evening my friends, i have an unusual error; if my code loads these values from file it gives me error Attempted to access head(3); index out of bounds because numel(head)=1.
If it loads other values like these it works
The txt file is like that
1 1994 1 8 3 59 27 0 15 54 11
8.9 1
9.2 1
10.2 1
12.9 1
11.4 1
10.7 1
12.4 1
8.3 1
7.7 1
2.2 6
2.0 1
etc etc
I hope you have some ideas. Thanks so much for your help!
  3 Comments
Nicola
Nicola on 9 Feb 2014
The problem is i don't touch anything, with some line of txt it works. Any idea? Because
1 1994 1 8 3 59 27 0 15 54 11
doen't work
but
5 1994 4 8 9 14 40 0 31 16 5
works
Michael
Michael on 12 Aug 2015
I had the same problem. Turns out somehow sum2str got declared as a variable. Subsequently matlab tries to access the 1st entry of the variable (which didn't exist). Hence the error.

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 12 Feb 2014
The error could be further down, such as near the end of file.
Try switching to
thisline = fgetl(head);
if ~ischar(thisline); break; end %end of file
head = sscanf(thisline, '%d', 11);
if length(head) < 11
fprintf('warning: line has too few elements: |%s|', thisline);
continue;
end
  1 Comment
Nicola
Nicola on 12 Feb 2014
Edited: Nicola on 12 Feb 2014
Thanks for your help How can an error at the end affect on the beginning of the code if i use debugger?
The complete code is (freq=32)
function ssttest(freq)
%Whitesands
%elev is the elevation angle (deg)
%site latitude (deg)
lat=32.54%Withesands
%site altitude above mean sea level (m)
Hamsl=1463%Whitesands
%polarization angle (deg)
pola=45;
arr=2;
z=2;
temp=2;
camp=6;
temp2=1;
partenza=1;
n_elev=1;
part=0;
ultimo=0;
fine=0;
cond=0;
evento_pioggia=0;
ffne=0;
d=1;
scrivi=0;
colori=cool(10);
cas=randi(100);
delete('Eventi_anno/*.txt');
year=['1994';'1995';'1996';'1997';'1998'];
res='60';
[ny,ans]=size(year); %ny=5 ans=4
[nr,ans]=size(res); %nr=1 ans=2
pathin='BancaDati/';
pathout='RisSSTtest/';
TtotRain=0;
Ttot=0;
for ir=1:nr,
deltat=str2num(res(ir,:));
for iy=1:ny,
nev=0;
name=[pathin,'r',year(iy,3:4),res(ir,:),'.txt'];
fid=fopen(name,'r');
while ~feof(fid)
head=fscanf(fid,'%d',11);
if ~isempty(head)
giorno=head(3);
mese=head(4); %prende il mese
ora=head(5);
minuti=head(6);
durata_h=head(8);
durata_m=head(9);
m=head(4);
tsR=[];
%vedere altezza sole in base ad ora
nname=['Risultatigiorno/',num2str(giorno),'_',num2str(mese),'.txt'];
nfid=fopen(nname,'r');
while ~feof(nfid)
hhead=fscanf(nfid,'%f',2);
if ~isempty(hhead)
tempo=hhead(1);
oora(arr)=fix(tempo);
mmminuti(arr)=abs(tempo-oora(arr));
mmminuti(arr)=mmminuti(arr)*100;
mminuti(arr)=int32(mmminuti(arr));
if mminuti(arr)==60 %risolve problema minuti .06
mminuti(arr)=mminuti(arr)/10;
end
gggradi(arr)=hhead(2);
ggradi(arr)=fix(gggradi(arr));
% if cond==0 && durata_h>=0 && durata_h<24; %guardo quante ore e minuti dura
% n_inc=ceil((durata_m/camp)+(10*durata_h)); %approssimo per eccesso
% end
if cond==1 && temp<=fine %gradi utili successivi al primo
g_elev(n_elev)=ggradi(arr-1); %forse non mi prende l'ultimo valore
on_elev=n_elev;
n_elev=n_elev+1;
if temp==fine
cond=0; %nel caso mi accorgessi che salta ultimo valore, metto qui un g_elev(n_elev)=ggradi(arr-1); n_elev=n_elev+1;
ultimo=1;
scrivi=1;
end
temp=temp+1;
temp2=temp2+1;
end
% if temp<=fine && n_elev==2 %evita il problema se ho solo 1 campione 1? evento
% cond=0;
% end
%
if ora==oora(arr) && minuti >= mminuti(arr-1) && minuti < mminuti(arr) %inizio evento
g_elev(n_elev)=ggradi(arr-1);
on_elev=n_elev;
n_elev=n_elev+1;
cond=1;
scrivi=0;
one=1;
temp2=1;
evento_pioggia=evento_pioggia+1;
fnamea=['Eventi_anno/',year(iy,3:4),'.txt'];
fidone=fopen(fnamea,'a');
fprintf(fidone,num2str(evento_pioggia));
fprintf(fidone,'\n');
fclose(fidone);
%temp=temp+1; %toglie il problema se ho solo 1 campione
end
if cond==1 && one==1
for ii=1:head(11), %fa scorrere per tutti gli eventi di pioggia?d
aux1=fscanf(fid,'%f',1); %prende la colonna dell'intensit? della pioggia
aux2=fscanf(fid,'%d',1); %prende la colonna del numero di eventi
tsR=[tsR zeros(1,aux2)+aux1]; %crea un array di 1 riga, lungo aux2 e ci mette dentro tante volte il valore di aux1
end
[A,t,deltaTa]=SyntStormEM(tsR,str2num(res),lat,Hamsl,freq,pola,g_elev(on_elev)); %mi serve lanciarlo per vedere n_inc
if n_elev==2
li=length(t);
n_inc=ceil(t(li)/(camp*60));
end
fine=partenza+n_inc;
if partenza==1
fine=fine-1;
end
one=0;
end
if cond==1 || ultimo==1
ultimo=0;
disp([res(ir,:),' - y: ',year(iy,:),' - m: ',num2str(m),' / ',num2str(head(1))]);
[A,t,deltaTa]=SyntStormEM(tsR,str2num(res),lat,Hamsl,freq,pola,g_elev(on_elev));%usa la nuova Hr di ITU-R
% %Grafico attenuazione x controllo
% figure(n_elev)
% leg=g_elev(on_elev);
% legend(num2str(leg))
% hold on;
% plot(t,A,'Color', colori(temp,:)) %gi? in secondi
% xlabel('Tempo (s)')
% ylabel('Attenuazione (dB)')
% pause(1)
TtotRain=TtotRain+length(tsR);
Ttot=Ttot+length(t);
fname=[pathout,'SST_WHS19_',year(iy,3:4),'XX',num2str(evento_pioggia,'%04.0f'),'.dat'];
fido=fopen(fname,'a');
fprintf(fido,'%%Channel model: SST, Sampling rate: %4.2f sample/s, Station name: White Sands, Link elevation: 37.8?.\n',1/(t(2)-t(1)));
fprintf(fido,'\n');
fne=(camp*(temp-1)*60);
% if fne>length(t)
% fne=length(t);
% end
for c=1:1:length(A)
if t(1,c)>=part && t(1,c)<=fne
fprintf(fido,'%6.3f\n',A(c));
y(d)=A(c);
x(d)=(t(c));
ffne=ffne+1;
d=d+1; %devo vedere dove azzerarlo a 1 /fatto
end
end
% %stampa grafico pezzi
% figure(999)
% plot(x,y)
% axis square
% xlabel('Tempo (s)')
% ylabel('Attenuazione (dB)')
if scrivi==1 %scrivilo su file
f = figure('Visible', 'off');
plot(x,y,'-','linewidth',1);
xlabel('Tempo (s)')
ylabel('Attenuazione (dB)')
saveas(f,['Grafici/Totale/Grafico_totale_evento',num2str(head(1)), '_del_' num2str(head(3)),'-',num2str(head(4)),'-',num2str(head(2)),'.jpeg']);
scrivi=0;
clear x
clear y
d=1;
end
part=fne;
fclose(fido);
end
arr=arr+1;
end
end
fclose(nfid);
if temp2<fine %tramonta ma continua a piovere %verifica se spostare
manca=fine-temp2;
fine=fine-manca;
end
if fine~=0 %controlla se serve e va qui
partenza=1;
end
% %funzione che salva in automatico i grafici
% f = figure('Visible', 'off');
% plot(t,A,'r');
% saveas(f,['Grafici/',num2str(elev),'_Evento_',num2str(head(1)), '_del_' num2str(head(3)),'-',num2str(head(4)),'-',num2str(head(2)),'.jpeg']);
end
end
fclose(fid);
end
end
deltaTA=t(2)-t(1);
save SSTTimeInfo9400 TtotRain Ttot deltaTA
if you want to try the code create a txt called r9460.txt and paste this text
1 1994 1 8 3 59 27 0 15 54 11
8.9 1
9.2 1
10.2 1
12.9 1
11.4 1
10.7 1
12.4 1
8.3 1
7.7 1
2.2 6
2.0 1
2 1994 2 8 0 32 30 0 22 22 15
17.2 1
3.3 4
2.3 1
2.3 5
15.3 1
56.9 1
17.9 1
18.7 1
24.3 1
12.5 1
9.1 1
6.3 2
7.1 1
7.5 1
2.7 1
3 1994 2 8 22 56 18 0 4 5 5
15.8 1
12.2 1
33.6 1
28.0 1
1.8 1
4 1994 3 8 0 41 7 0 45 59 34
31.3 1
90.6 1
119.4 1
126.4 1
93.9 1
110.6 1
119.3 1
116.2 1
86.4 1
104.1 1
114.3 1
144.8 1
94.9 1
72.7 1
80.3 1
84.3 1
76.7 1
63.0 1
59.2 1
46.1 1
33.5 1
20.6 1
15.9 1
7.9 1
7.6 1
5.4 1
5.3 1
5.1 1
3.0 4
3.0 1
2.7 5
2.5 1
2.1 6
2.0 1
5 1994 4 8 9 14 40 0 31 16 5
0.6 1
0.6 25
0.9 1
3.5 4
0.9 1
6 1994 14 8 23 37 16 0 12 28 12
15.9 1
12.9 1
18.9 1
24.9 1
22.6 1
15.9 1
10.0 1
4.8 1
4.3 2
5.9 1
8.2 1
3.8 1
7 1994 15 8 13 35 24 1 4 25 5
0.4 1
0.4 37
0.4 1
0.6 25
0.2 1
8 1994 16 8 1 48 41 0 2 3 3
7.4 1
7.4 1
0.4 1
9 1994 23 8 19 26 30 0 20 22 10
19.5 1
22.7 1
17.2 1
3.9 1
2.5 5
2.8 1
2.8 4
2.7 1
2.6 5
1.0 1
10 1994 30 8 0 36 14 3 25 50 56
23.6 1
24.3 1
25.7 1
33.4 1
21.2 1
19.2 1
21.8 1
27.6 1
17.0 1
16.2 1
9.3 1
5.3 1
1.7 8
8.8 1
14.1 1
18.4 1
24.0 1
12.3 1
2.3 6
5.3 1
8.1 1
9.0 1
9.4 1
4.9 3
0.6 1
0.3 56
9.3 1
19.5 1
30.8 1
28.1 1
30.9 1
33.7 1
26.8 1
24.2 1
34.5 1
36.5 1
35.4 1
45.2 1
51.3 1
27.7 1
27.6 1
56.7 1
44.5 1
31.8 1
3.5 4
1.9 1
1.0 14
0.8 1
0.4 35
0.5 1
1.1 14
1.5 1
1.5 9
1.3 1
1.2 11
1.0 1
11 1994 31 8 4 30 38 0 33 29 11
40.9 1
42.9 1
39.4 1
20.0 1
12.1 1
10.6 1
5.0 1
4.0 2
3.9 1
0.6 23
0.3 1
12 1994 3 9 23 44 38 1 8 35 15
1.2 1
1.2 11
1.0 1
0.9 17
3.6 1
3.8 3
2.9 1
2.9 4
1.8 1
1.2 12
1.4 1
1.7 8
1.9 1
2.1 6
1.2 1
13 1994 4 9 22 18 53 0 42 49 9
6.5 1
6.5 1
4.0 1
2.6 5
0.9 1
0.7 20
0.7 1
1.2 12
1.0 1
14 1994 8 9 21 30 41 0 18 23 19
11.6 1
13.6 1
17.4 1
26.3 1
26.6 1
26.5 1
23.5 1
22.7 1
34.1 1
31.6 1
32.2 1
33.5 1
37.7 1
35.3 1
34.7 1
34.2 1
38.1 1
18.4 1
5.1 1
15 1994 14 10 11 37 35 0 36 23 7
0.6 1
0.6 25
2.1 1
9.8 1
4.9 1
1.9 7
0.7 1
16 1994 15 10 2 48 1 2 10 30 41
3.7 1
3.7 3
2.6 1
2.5 5
1.6 1
1.5 9
6.4 1
6.5 1
3.8 1
2.2 6
0.9 1
0.3 47
23.4 1
11.6 1
6.1 1
5.1 1
1.8 8
17.7 1
18.2 1
23.0 1
29.0 1
26.5 1
23.7 1
15.7 1
10.5 1
31.9 1
35.4 1
28.6 1
33.3 1
16.9 1
18.5 1
25.5 1
28.7 1
20.2 1
9.1 1
1.3 11
1.5 1
1.5 9
29.6 1
39.6 1
7.6 1
17 1994 3 11 1 26 1 0 29 59 15
22.4 1
24.9 1
15.8 1
18.1 1
20.3 1
23.1 1
16.4 1
10.3 1
17.2 1
5.5 2
4.0 1
1.5 9
1.6 1
1.9 7
1.8 1
18 1994 8 11 14 30 15 0 33 11 3
0.5 1
0.5 32
0.1 1
19 1994 11 11 17 36 51 1 17 54 5
0.5 1
0.5 29
0.4 1
0.3 46
0.3 1
20 1994 11 11 21 35 55 0 19 10 3
0.8 1
0.8 18
0.1 1
21 1994 11 11 23 31 42 0 58 51 33
1.4 1
1.4 9
2.1 1
3.1 4
2.9 1
2.8 5
5.0 1
5.2 2
6.6 1
6.6 1
7.1 1
7.3 1
7.1 1
7.0 1
7.5 1
8.1 1
7.5 1
7.0 1
5.8 1
3.8 3
4.1 1
4.8 2
4.8 1
5.1 2
5.0 1
5.0 2
5.0 1
4.9 3
4.0 1
4.0 2
4.0 1
3.8 3
3.2 1
22 1994 12 11 3 54 31 1 7 20 9
2.1 1
2.1 6
1.3 1
0.8 17
0.7 1
0.5 32
0.6 1
1.8 8
0.6 1
23 1994 12 11 7 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
3.2 4
2.7 1
2.6 4
2.7 1
2.8 5
3.6 1
3.8 3
2.3 1
1.8 7
1.4 1
33 1994 27 12 16 41 20 0 17 42 7
1.8 1
1.8 7
2.3 1
3.0 4
3.2 1
3.8 3
2.7 1
Thanks so much for the help

Sign in to comment.


Nicola
Nicola on 13 Feb 2014
is anyone able to help me? :(
  2 Comments
Rizwana
Rizwana on 14 Feb 2014
Edited: Rizwana on 14 Feb 2014
Iam beginner in matlab. Best way to know why there is errors is run each command by yourself specially when the code is written by someone else. Moreover comments in the code are not in english. Iam not able to understand what it is doing.
Nicola
Nicola on 19 Feb 2014
You are right, so i simplified the code

Sign in to comment.


Nicola
Nicola on 19 Feb 2014
Guys, i simplified the problem The code is:
name=['r9460.txt']
fid=fopen(name,'r');
while ~(feof(fid)),
head=fscanf(fid,'%d',11)
if ~(isempty(head)),
m=head(4)
end
end
fclose(fid)
The txt is
5 1994 4 8 9 14 40 0 31 16 5
0.6 1
0.6 25
0.9 1
3.5 4
0.9 1
Matlab says Attempted to access head(4); index out of bounds because numel(head)=1.
Please, help me, i'm in your hands!
  1 Comment
Walter Roberson
Walter Roberson on 12 Aug 2015
head=fscanf(fid,'%d',11) tells MATLAB to read 11 elements even if it requires reading multiple lines to do so. The first line of the file has 11 elements and so works, but then multiple lines get read for the next fscanf(). Reading will leave off even in the middle of a text line if that is what is needed to read 11 items, and the next fscanf() would pick up in the middle of a line if need be. At some point you are ending up at the end of file with a line which is not empty but which does not have 4 items to index.
If you have a mix of line formats that imply different data structures then you need a mix of fscanf() calls. If you do not know ahead of time which data structure a line is going to be for until you read the line and count the number of items, then you need to read the line as a string, then use sscanf() to parse the string to find out how many items are on the line and react accordingly.
Any time the line format has to be examined to determine something about how the data should be interpreted, you should be reading a line at a time use fgetl() or fgets() and examining the line.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!