MATLAB: Am I recieving a ‘Subscript indices must either be real positive integers or logicals’ error message?

Error message in the line computing pick
I'm trying to pick the first arrival of a seismic trace. The code finds where the gradient of the trace is small before the first peak.
I dont know why i'm getting the error message as this code worked when I wasnt looping over all my 48 traces.
N=48; picks = zeros(N,1); firstpks= zeros(N,1);
for i=1:N
[pospks,poslocs] = findpeaks(NewData_NORM1(:,i),'minpeakheight',0.1,'minpeakdistance',10); % find all peaks [negpks,neglocs] = findpeaks(-NewData_NORM1(:,i),'minpeakheight',0.1,'minpeakdistance',10);
negpks= -negpks;
tempgrad = gradient(NewData_NORM1(:,i)); % local gradients refgrad = (negpks(1)-pospks(1))/(neglocs(1)-poslocs(1));
pick = poslocs(1)-(neglocs(1)-(poslocs(1)))+ max(find(abs(tempgrad((poslocs(1)-(neglocs(1)-poslocs(1))):(poslocs(1)-(neglocs(1)-poslocs(1))/4))) < abs(refgrad/12)));
% search of a gradient lower than something specified by user (related to the trough to peak gradient) within the interval starting one period before the neg peak and finishing one quarter of a period before it
picks(i,1) = pick;
end

Best Answer

Do you know how to use the debugger? Set a breakpoint on the line with the error. Then make sure no array indexes (whether a single variable or the result of an expression like a subtraction) are zero or negative, or fractional numbers like 6.42. Those are not allowed as array indexes.