What is happening here?
Show older comments
Hi there! I am modifying a working script and I keep getting an error. By now I do not know what else to do (it is true that I don't know a lot about programming). Both scripts are really similar. They are made to analyze data from a behavioral computerized experiment. The script is telling matlab to extract the number of times that some pattern of stimuli happen (for example in the first script, tell me how many times an auditory stimuli type A -15- is followed by a visual stimuli type A -51- and this one is then followed by a correct response -111 or 112-, and the time between the visual stimuli and its response is as maximum 2000 ms. The script also asks (two lines below the previous) for the time between the correct response and the visual stimuli.
So, this example is in script 1, that is fully functional. But now I need to look for much more complex patterns of stimuli (see script 2). For example, the first condition I ask for says that I want to look for conditions where there is a visual stimuli type A or B or C or D or E or F -41,42,43,44,45,46- followed by no response and then followed by another visual stimuli of the same type that is followed by a correct response (111 or 112). The thing is that half of the patterns are extracted perfectly (the first 4 statements work perfectly and the data I need is showed correctly. But when it arrives to the fifth (those that have the comments starting with %cond3Cs:, %cond4Cs:, %cond5Cs:, and, %cond5Cs:), it says that "Index exceeds array bounds.".
So, here is Script 1 (fully working):
for x=1;
sujeto=[1007]; %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:C3000');
tiempo2=xlsread(filename,'D6:D3000');
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));
resultado_cond5Cs=zeros(size(estimulo));
resultado_cond6Cs=zeros(size(estimulo));
for i = 1:length(estimulo)
% descripcion de los codigos para cada condicion
if (estimulo(i)==15) && (estimulo(i+1)==51 && (estimulo(i+2)==111 || estimulo(i+2)==112) && tiemposus (i+2) <= 2000)
resultado_cond1Cs(i)=1;
result_tr_cond1Cs(i)=tiemposus(i+1);
elseif (estimulo(i)==25) && (estimulo(i+1)==52 && (estimulo(i+2)==111 || estimulo(i+2)==112) && tiemposus (i+2) <= 2000)
resultado_cond2Cs(i)=1;
result_tr_cond2Cs(i)=tiemposus(i+1);
elseif (estimulo(i)==35) && (estimulo(i+1)==53 && (estimulo(i+2)==111 || estimulo(i+2)==112) && tiemposus (i+2) <= 2000)
resultado_cond3Cs(i)=1;
result_tr_cond3Cs(i)=tiemposus(i+1);
elseif (estimulo(i)==16) && (estimulo(i+1)==61 && (estimulo(i+1)== estimulo(end) || estimulo(i+2)~=11 || estimulo(i+2)~=12))
resultado_cond4Cs(i)=1;
elseif (estimulo(i)==26) && (estimulo(i+1)==62 && (estimulo(i+1)== estimulo(end) || estimulo(i+2)~=11 || estimulo(i+2)~=12))
resultado_cond5Cs(i)=1;
elseif (estimulo(i)==36) && (estimulo(i+1)==63 && (estimulo(i+1)== estimulo(end) || estimulo(i+2)~=11 || estimulo(i+2)~=12))
resultado_cond6Cs(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),:)=[];
resultado_cond5Cs(~any(resultado_cond5Cs,2),:)=[];
resultado_cond6Cs(~any(resultado_cond6Cs,2),:)=[];
NESTGO=length(resultado_cond1Cs);
TRESTGO=mean(result_tr_cond1Cs);
NDEVGO=length(resultado_cond2Cs);
TRDEVGO=mean(result_tr_cond2Cs);
NNOVGO=length(resultado_cond3Cs);
TRNOVGO=mean(result_tr_cond3Cs);
NESTNOGO=length(resultado_cond4Cs);
NDEVNOGO=length(resultado_cond5Cs);
NNOVNOGO=length(resultado_cond6Cs);
%crear fila con valores del sujeto
a= [NESTGO,NDEVGO,NNOVGO,NESTNOGO,NDEVNOGO,NNOVNOGO,TRESTGO,TRDEVGO,TRNOVGO];
Z(x,:)= a;
x=x+1;
clear a
clear result*
clear i
clear tiempo2
end
end
Here is Script 2 (the one with the error):
for x=1
sujeto=[1007]; %poner los codigos de sujeto entre corchetes
path='C:\Users\so_sana\Documents\MATLAB\Ficheros_excel\'; %set path for the ecel file(s), 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)
% description of codes for each condition
%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);
%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;
%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;
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
Here is the error to Script 2:

And attached you can find an eample datafile from one subject (1007, the one used for testing both scripts).
Hope you can help me!!
7 Comments
Rik
on 6 Jan 2019
Susana Cid Fernández
on 6 Jan 2019
Edited: Susana Cid Fernández
on 6 Jan 2019
Rik
on 6 Jan 2019
Did you try running my code? It was self-contained and showed you how you could replace those huge lists of condition, as well as how to find a pattern. I think it is better to re-think bulky code, than to apply a hack that would push the problem into the future.
I don't think this post should be deleted, but I don't think that other post should be deleted either (which may happen 30 days after closing).
For now, I would urge you to look into the advice you have gotten so far on this thread, especially that of Image Analyst.
Stephen23
on 7 Jan 2019
Duplicate (with ignored answer):
Susana Cid Fernández
on 7 Jan 2019
Edited: Susana Cid Fernández
on 7 Jan 2019
Susana Cid Fernández
on 8 Jan 2019
Image Analyst
on 8 Jan 2019
You're welcome Susana. Could you then go down to my Answer and click "Accept this answer"? Thanks in advance. Accepting Answers gives answerers "credit" for the answer in terms of reputation points.
Accepted Answer
More Answers (1)
per isakson
on 6 Jan 2019
Edited: per isakson
on 6 Jan 2019
Proposal: Set a breakpoint at the line that throws the error. Run the code again. At the breakpoint, inspect the values of the variables, which occur in the line, which throws the error.
Or instead of setting a breakpoint run these commands before you run your code
dbstop if error
dbstop if caught error
1 Comment
Susana Cid Fernández
on 7 Jan 2019
Categories
Find more on Matrix Indexing 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!