MATLAB: Index exceeds matri dimensions

help

Hi all! I keep getting an error: Index exceeds array bounds. Here is the script
for x=1
sujeto=[1009, 1013, 1014, 1015, 1017]; %poner los codigos de sujeto entre corchetes
path='C:\Users\so_sana\Documents\MATLAB\Ficheros_excel\'; %poner la ruta de la carpeta con los ficheros excel, no olvidar la ultima barra
for s=sujeto
ss=num2str(s);
task ='av';
filename=[path ss 'av.xls']; %se indica que fichero va a ser analizado
estimulo=xlsread(filename,'C6:C4000');
tiempo2=xlsread(filename,'D6:D4000');
tiempo=tiempo2/10;
tiemposus=zeros(size(tiempo));
for i = 3:length(tiempo)-1
tiemposus (i)=tiempo(i+1)-tiempo(i);
end
resultado_cond1Cs=zeros(size(estimulo));
result_tr_cond1Cs=resultado_cond1Cs;
resultado_cond2Cs=zeros(size(estimulo));
result_tr_cond2Cs=resultado_cond2Cs;
resultado_cond3Cs=zeros(size(estimulo));
result_tr_cond3Cs=resultado_cond3Cs;
resultado_cond4Cs=zeros(size(estimulo));
result_tr_cond4Cs=resultado_cond4Cs;
resultado_cond5Cs=zeros(size(estimulo));
resultado_cond6Cs=zeros(size(estimulo));
resultado_cond7Cs=zeros(size(estimulo));
resultado_cond8Cs=zeros(size(estimulo));
for i = 1:length(estimulo)
% descripcion de los codigos para cada condicion
%cond1Cs: Go (miss): 41/42/43/51/52/53 sin respuesta, seguida de auditivo con Go (correcta):41/42/43/51/52/53 seguido de 111/112
if (estimulo(i)==41 || estimulo(i)==42 || estimulo(i)==43 || estimulo(i)==51 || estimulo(i)==52 || estimulo(i)==53) && (estimulo (i)== estimulo(end)) && (estimulo(i+1)==14 || estimulo(i+1)==15 ||estimulo(i+1)==24 || estimulo(i+1)==25 ||estimulo(i+1)==34 || estimulo(i+1)==35) && (estimulo(i+2)==41 || estimulo(i+2)==42 || estimulo(i+2)==43 || estimulo(i+2)==53 || estimulo(i+2)==51 || estimulo(i+2)==52) && (estimulo(i+3)==111 || estimulo(i+3)==112)&& tiemposus (i+3) <= 2000
resultado_cond1Cs(i)=1;
result_tr_cond1Cs(i)=tiemposus(i+2);
%cond2Cs: Go (corr/incorr): 41/42/43/51/52/53 seguida de R 111/112/11/12, seguida de auditivo con Go (correcta):41/42/43/51/52/53 seguido de 111/112
elseif (estimulo(i)==41 || estimulo(i)==42 || estimulo(i)==43 || estimulo(i)==51 || estimulo(i)==52 || estimulo(i)==53) && (estimulo (i+1)==111 || estimulo(i+1)==112 || estimulo(i+1)==11 || estimulo(i+1)==12) && (estimulo(i+2)==14 || estimulo(i+2)==15 ||estimulo(i+2)==24 || estimulo(i+2)==25 ||estimulo(i+2)==34 || estimulo(i+2)==35) && (estimulo(i+3)==41 || estimulo(i+3)==42 || estimulo(i+3)==43 || estimulo(i+3)==53 || estimulo(i+3)==51 || estimulo(i+3)==52) && (estimulo(i+4)==111 || estimulo(i+4)==112) && tiemposus (i+4) <= 2000
resultado_cond2Cs(i)=1;
result_tr_cond2Cs(i)=tiemposus(i+3);
%cond3Cs: NoGo (correctas): 61/62/63 sin R, seguida de auditivo 14/15/24/25/34/35 con Go (correcta):41/42/43/51/52/53 seguido de 111/112
elseif (estimulo(i)==61 || estimulo(i)==62 || estimulo(i)==63) && (estimulo (i)== estimulo(end)) && (estimulo(i+1)==14 || estimulo(i+1)==15 ||estimulo(i+1)==24 || estimulo(i+1)==25 || estimulo(i+1)==34 ||estimulo(i+1)==35) && (estimulo(i+2)==41 || estimulo(i+2)==42 || estimulo(i+2)==43 || estimulo(i+2)==53 || estimulo(i+2)==51 || estimulo(i+2)==52) && (estimulo(i+3)==111 || estimulo(i+3)==112)&& tiemposus (i+3) <= 2000
resultado_cond3Cs(i)=1;
result_tr_cond3Cs(i)=tiemposus(i+2);
%cond4Cs: NoGo (con R incorrecta): 61/62/63 con R 11/12, seguida de auditivo 14/15/24/25/34/35 con Go (correcta):41/42/43/51/52/53 seguido de 111/112
elseif (estimulo(i)==61 || estimulo(i)==62 || estimulo(i)==63) && (estimulo (i+1)==11 || estimulo(i+1)==12) && (estimulo(i+1)==14 || estimulo(i+1)==15 ||estimulo(i+1)==24 || estimulo(i+1)==25 || estimulo(i+1)==34 ||estimulo(i+1)==35) && (estimulo(i+3)==41 || estimulo(i+3)==42 || estimulo(i+3)==43 || estimulo(i+3)==53 || estimulo(i+3)==51 || estimulo(i+3)==52) && (estimulo(i+4)==111 || estimulo(i+4)==112)&& tiemposus (i+4) <= 2000
resultado_cond4Cs(i)=1;
result_tr_cond4Cs(i)=tiemposus(i+3);
%cond5Cs: NoGo (correctas) 61/62/63 sin R, seguida de auditivo 16/26/36 con NoGo (correcta): 61/62/63 sin R
elseif (estimulo(i)==61 || estimulo(i)==62 || estimulo(i)==63) && (estimulo (i)== estimulo(end)) && (estimulo(i+1)==16 || estimulo(i+1)==26 ||estimulo(i+1)==36) && (estimulo(i+2)==61 || estimulo(i+2)==62 || estimulo(i+2)==63) && (estimulo (i+2)== estimulo(end))
resultado_cond5Cs(i)=1;
%cond6Cs: NoGo (con R incorrecta): 61/62/63 con R 11/12, seguida de auditivo 16/26/36 con NoGo (correcta): 61/62/63 sin R
elseif (estimulo(i)==61 || estimulo(i)==62 || estimulo(i)==63) && (estimulo (i+1)==11 || estimulo(i+1)==12) && (estimulo(i+2)==16 || estimulo(i+2)==26 ||estimulo(i+2)==36) && (estimulo(i+3)==61 || estimulo(i+3)==62 || estimulo(i+3)==63) && (estimulo (i+3)== estimulo(end) || estimulo(i+4)~=11 || estimulo(i+4)~=12)
resultado_cond6Cs(i)=1;
%cond7Cs: Go (miss): 41/42/43/51/52/53 sin respuesta, seguida de auditivo 16/26/36 con NoGo (correcta): 61/62/63 sin R
elseif (estimulo(i)==41 || estimulo(i)==42 || estimulo(i)==43 || estimulo(i)==51 || estimulo(i)==52 || estimulo(i)==53) && (estimulo (i)== estimulo(end))&& (estimulo(i+1)==16 || estimulo(i+1)==26 ||estimulo(i+1)==36) && (estimulo(i+2)==61 || estimulo(i+2)==62 || estimulo(i+2)==63) && (estimulo (i+2)== estimulo(end) || estimulo(i+3)~=11 || estimulo(i+3)~=12)
resultado_cond7Cs(i)=1;
%cond8Cs: Go (corr/incorr): 41/42/43/51/52/53 seguida de R 111/112/11/12, seguida de auditivo 16/26/36 con NoGo (correcta): 61/62/63 sin R
elseif (estimulo(i)==41 || estimulo(i)==42 || estimulo(i)==43 || estimulo(i)==51 || estimulo(i)==52 || estimulo(i)==53) && (estimulo (i+1)==111 || estimulo(i+1)==112 || estimulo(i+1)==11 || estimulo(i+1)==12) && (estimulo(i+2)==16 || estimulo(i+2)==26 ||estimulo(i+2)==36) && (estimulo(i+3)==61 || estimulo(i+3)==62 || estimulo(i+3)==63) && (estimulo (i+3)== estimulo(end) || estimulo(i+4)~=11 || estimulo(i+4)~=12)
resultado_cond8Cs(i)=1;
end
end
%crear una columna con los valores de cada condicion
resultado_cond1Cs(~any(resultado_cond1Cs,2),:)=[];
result_tr_cond1Cs(~any(result_tr_cond1Cs,2),:)=[];
resultado_cond2Cs(~any(resultado_cond2Cs,2),:)=[];
result_tr_cond2Cs(~any(result_tr_cond2Cs,2),:)=[];
resultado_cond3Cs(~any(resultado_cond3Cs,2),:)=[];
result_tr_cond3Cs(~any(result_tr_cond3Cs,2),:)=[];
resultado_cond4Cs(~any(resultado_cond4Cs,2),:)=[];
result_tr_cond4Cs(~any(result_tr_cond4Cs,2),:)=[];
resultado_cond5Cs(~any(resultado_cond5Cs,2),:)=[];
resultado_cond6Cs(~any(resultado_cond6Cs,2),:)=[];
resultado_cond7Cs(~any(resultado_cond7Cs,2),:)=[];
resultado_cond8Cs(~any(resultado_cond8Cs,2),:)=[];
NoGOGO1=length(resultado_cond1Cs);
TRGOGO1=mean(result_tr_cond1Cs);
NoGOGO2=length(resultado_cond2Cs);
TRGOGO2=mean(result_tr_cond2Cs);
NoNOGOGO1=length(resultado_cond3Cs);
TRNOGOGO1=mean(result_tr_cond3Cs);
NoNOGOGO2=length(resultado_cond4Cs);
TRNOGOGO2=mean(result_tr_cond4Cs);
NoNOGONOGO1=length(resultado_cond5Cs);
NoNOGONOGO2=length(resultado_cond6Cs);
NoGONOGO1=length(resultado_cond7Cs);
NoGONOGO2=length(resultado_cond8Cs);
%crear fila con valores del sujeto
a= [NoGOGO1,TRGOGO1,NoGOGO2,TRGOGO2,NoNOGOGO1,TRNOGOGO1,NoNOGOGO2,TRNOGOGO2,NoNOGONOGO1,NoNOGONOGO2,NoGONOGO1,NoGONOGO2];
Z(x,:)= a;
x=x+1;
clear a
clear result*
clear i
clear tiempo2
end
end
Line 53 is the one below %cond3Cs. If I "cancel" (with %) the three lines below %cond3Cs, the error is then below %cond4Cs. And so on for %cond5Cs and %cond6Cs. Instead, the code below %cond7Cs and %cond8Cs (as the one below %cond1Cs and %cond2Cs) runs perfectly. As I suppose you can see, I am not a pro, and I don't know what else to do. I hope you can help me.
Thank you in advance!!

Best Answer

This has to do with your loop and with the way Matlab parses the || operator.
You loop goes to length(estimulo), but then you try to access estimulo(i+1), which doesn't exist.
The reason why this error may not occur every time is that Matlab parses each element you are entering in the or, and stops the execution when it finds a true. Since a single true is enough to set the entire statement to true, Matlab doesn't need to evaluate the rest.
Also, I would urge you to use parentheses to separate && and ||, which would make sure Matlab executes your tests in the order you want it to.
clear ff %make sure ff doesn't exist
a=[true false];
for ii=1:2
if a(ii) || ff %will return error when a(ii) is false
disp(ii)
end
end