Error Index exceeds the number of array elements (0)

1 view (last 30 days)
Hi all,
My code seems to have a problem but i'm not able to overcome it.
I have my stimulation signal (stimp). With findpeak i found a reference point.
My goal is to check from the referecne point, going backward 5 points, which is the first point which my signal goes below a certain treshould (0.5)
Code below:
[pks,loc]=findpeaks(stimp,'MinPeakHeight',2); %find peaks of stimulation bigger than 2mA
%code to detect the first peak
locp = loc(1);
k = 1;
for i = 2:numel(loc) %keep only first peak that has distance to next other peak >6samples
if abs(loc(i)-loc(i-1))>6
k = k+1;
locp(k) = loc(i);
end
end
%code to detect the first point before the first peak that is smaller than
%0.5
for i = 1:length(locp) %Starting from the peak, backward of 5 samples, find the first sample before the peak smaller than the threshold=0.5mA
ind1=locp(i):-1:locp(i)-5;
segment = stimp(ind1);
ind= find(segment<0.5);
lock(i) = ind1(ind(1));
end
I don't understand because sometimes is working, some other times no
the error is:
Index exceeds the number of array elements (0).
Error in Untitled (line 9)
lock(i) = ind1(ind(1));
Please find attached a copy of the stimulation signal= stimp
Can anyone help me with this?
Thanks a lot for your time
Regards,
Francesco

Accepted Answer

Jan
Jan on 6 Sep 2021
ind = find(segment < 0.5);
lock(i) = ind1(ind(1));
If there is no element in segment, which is smaller than 0.5, ind is empty. Then you cannot access ind(1).
I cannot guess, what you want to write into lock(i) in this case.
  2 Comments
Francesco Lucarelli
Francesco Lucarelli on 6 Sep 2021
i want to write the location of the element before the peak that is smaller than 0.5
It's weird that is empty because i can find the peak reference point, and there are for sure points before that peak which are smaller than 0.5
Do you think my code is inefficient in that way?
Thanks for your answer
Francesco Lucarelli
Francesco Lucarelli on 6 Sep 2021
Edited: Francesco Lucarelli on 6 Sep 2021
Thanks a lot for your help.
I increased the number of points to check before the peak, and i solved the issue.
Thanks
Regards,
Francesco

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!