Your idea of increasing 'MinPeakDistance' to adjust the peaks found is not consistent.
For example, if you iterate manually, you can see that when length(max_val) is approaching length(t0_pos1), you are already starting to lose some valuable information as shown below in green.
There was a related question yesterday. You can use the method explained there, which is originally from this question to divide peaks in group. t = datetime(2021,1,1,1:length(EFFEKT),0,0)';
ad = EFFEKT' > Abonnemang;
ad0(logical(ad)) = find(ad) ;
ii(strfind([0,jj(:)'],[0 1])) = 1;
out = accumarray( idx(jj)',ad0(jj)',[],@(x){x'});
peakTime = NaT(1,nPeaks);
firstCrossing = NaT(1,nPeaks);
lastCrossing = NaT(1,nPeaks);
[maxValue, maxIdx] = max(EFFEKT(out{j}));
peakTime(j) = t(out{j}(maxIdx));
firstCrossing(j) = t(out{j}(1));
lastCrossing(j) = t(out{j}(end));
scatter(peakTime,peaks,'r')
plotIdx = out{peakIdx}(1)-2:out{peakIdx}(end)+2;
plot(t(plotIdx),EFFEKT(plotIdx));
scatter(peakTime(peakIdx),peaks(peakIdx),'r')
xline(firstCrossing(peakIdx),'k')
xline(lastCrossing(peakIdx),'k')
Result:
On a sidenote, I suggest you to pay attention on what really depends on the looping variable, in your case x.
For example you are performing the 8760 times the command:
[t0_pos1,s0_pos1,t0_neg1,s0_neg1]= crossing_V7(EFFEKT,t,Abonnemang,'linear');
peak_width = t0_neg1- t0_pos1;
But this does not depend on x, therefore it may be performed only once prior to the loop, largely increasing the speed of your code.