What is happening here?

조회 수: 5 (최근 30일)
Susana Cid Fernández
Susana Cid Fernández 2019년 1월 6일
댓글: Image Analyst 2019년 1월 8일
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
Susana Cid Fernández
Susana Cid Fernández 2019년 1월 8일
Thak you all for you comments and help. Finally with Image Analyst's instructions the code works. But in the future I will use you advice Rik! Thank you so much for your patience.
Image Analyst
Image Analyst 2019년 1월 8일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2019년 1월 6일
편집: Image Analyst 2019년 1월 6일
Please follow my instructions from the other thread. Essentially i+3 is longer than one of the vectors. OK if you don't want to set a breakpoint and find out what it is, put this line just below the "for" line so it's the first in the loop
for i = 1:length(estimulo)
fprintf('Length of estimulo is %d and trying to access index %d\n', length(estimulo), i+3);
Tell me what you see: give me the last black line plus all the red text.
I mean, anyone can see right off the bat that since i goes to length(estimula) and you're trying to access 3 indexes beyond that, you'll get an error. If length(estimulo) is 100 and you're trying to access 100 + 3 = 103, it will throw an error because there is no index of 103 - that's beyond the end of the vector.
  댓글 수: 3
Image Analyst
Image Analyst 2019년 1월 7일
Maybe you should have your loop end 3 indexes earlier:
for i = 1 : length(estimulo) - 3
fprintf('Length of estimulo is %d and trying to access index %d\n', length(estimulo), i+3);
That way you won't go past the end of it.
Also, it's recommended not to use i (the imaginary variable) as your loop index. Use k or some other name.
Susana Cid Fernández
Susana Cid Fernández 2019년 1월 8일
Thank you so much, now it works!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

per isakson
per isakson 2019년 1월 6일
편집: per isakson 2019년 1월 6일
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
Susana Cid Fernández
Susana Cid Fernández 2019년 1월 7일
Thaks per. The error is now clear. Hope to fix it soon!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by