Index exceeds matri dimensions

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!!

2 Comments

What is the value of i+3, and what are the lengths of estimulo and tiemposus? This is just standard debugging. It clearly states that the index, i+3, is longer than the array so of course you need to know the value of the index and the length of the array.
Thanks for your response, but that shouldn't be the problem, as I get the error in the first line below %cond3Cs, and the index i+3 appears before (below %cond2Cs) and the code runs (I obtain correct data)...

Sign in to comment.

 Accepted 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

4 Comments

Hi there! Thank you a lot for your answer. Excuse me if i get something wrong (I am just an amateur here), but why do you say that estimulo(i+1) does not exist? It is already in the code before the error and works perfectly (see first line below %cond1Cs and %cond2Cs, for example).In one of those lines there are no true, meaning that the result is 0 or NaN, because in the data there is not that condition anywhere, it just does not exist. Probably I am not understanding something, I'm sorry. But it seems weird to me that the error is restricted to condCs 3, 4, 5 and 6, and that 1, 2, 7 and 8 work perfectly...
I would like to ask you also about the parentheses. I put them where I think they should be. I'll try to explain. In those statements, I am telling him to count number and reaction times of and to some stimuli in an experiment. For example:
%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
I want to count how many conditions are there that begin with a (let's call it) visual Go stimuli (codes = 41, 42, 43, 51, 52, 53, one of them is enough) that ate followed by no response, and then followed by one of the auditory stimuli (14, 15, 24, 25, 34, 35, again, any one is OK), and then followed by another visual Go stimuli (same codes as before) followed by a correct response (111 or 112). I want him also to tell me when this correct response has been made with respect to the precedent visual Go stimuli.
This lines work, and the results are correct. That is why I put parenthesis comprising the || statements and separating them from the &&. Do you think this is correct?
Again, I am really sorry, I feel completely lost here...
So you are actually looking for a pattern, why not do that instead?
You should also have a look at the ismember function. It is designed specifically to test these things.
codes=struct;%make a blank struct, overwriting a potentially existing one
codes.visual=[41, 42, 43, 51, 52, 53];
codes.auditory=[14, 15, 24, 25, 34, 35];
codes.correct=[111, 112];
estimulo=randi([30 50],150,1);
L=struct;%L for logical array
L.visual=ismember(estimulo,codes.visual);
L.auditory=ismember(estimulo,codes.auditory);
%you need to crop the arrays to compare A1 with B2, A2 with B3 etc
%A=[1 2 3 4 x]
%B=[x 1 2 3 4]
%this can be done with 1:(end-1) and 2:end
L.vis_then_aud=L.visual(1:(end-1)) & L.auditory(2:end);
pos=struct;
pos.vis_then_aud=find(L.vis_then_aud);
if any(L.vis_then_aud)
disp(pos.vis_then_aud)
else
disp('no matches')
end
If you want more specific help, you should edit your question and attach an example Excel file, so we can work with your actual data.
A last remark. I have the feeling that you try to find out if you are looking at the last element with (estimulo(i)== estimulo(end)). That is not what it does. To do that you should compare i with numel(estimulo).
Susana Cid Fernández comment posted as answer:
Thank you all for your patience and responses. I made a better post explaining what the script is for, and attaching an example file. Again, thank you so much. This is the link to the new post:
Thanks a lot!!
Why did you ignore my suggestions in the new thread? And didn't you link back to this one. I think it would have been a much better sollution to edit this question and continue the thread here.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Reopened:

Rik
on 6 Jan 2019

Community Treasure Hunt

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

Start Hunting!