I think the problem is that there is an implicit assumption in your code about whether the i-th peak comes before the i-th trough or the i-th trough comes before the i-th peak. That is to say, it is necessary to know whether all_peak_location(i) is the location of the peak right before or right after all_trough_location(i), but this depends on which comes first in the data, the first peak or the first trough.
To illustrate this point, first I'll run the code as posted to get the plot for reference:
[all_peak_value,all_peak_location]= findpeaks(thigh_orient_y);
[all_trough_value,all_trough_location]= findpeaks(-thigh_orient_y);
all_trough_value = -all_trough_value;
[main_trough_value,main_trough_location]= findpeaks(thigh_orient_y,'MinPeakProminence',30);
for i = 1:length(main_trough_location)
index = all_peak_location == main_trough_location(i);
start_location(counter) = all_trough_location(find(index)-1);
start_value(counter) = all_trough_value(find(index)-1);
for i = 1:length(main_trough_location)
index = all_trough_location > main_trough_location(i);
end_location(counter) = all_trough_location(find(index,1));
end_value(counter) = all_trough_value(find(index,1));
plot(all_trough_location,all_trough_value,'ko')
plot(main_trough_location,main_trough_value,'b*')
plot(start_location,start_value,'r*')
plot(end_location,end_value,'g*')
h = legend('signal',' all troughs','main peak','start', 'end');
set(h,'location','northeast','box','off')
Now look what happens when I remove the first 10 samples of data, so that the first trough is removed, but the first peak remains:
thigh_orient_y(1:10) = [];
[all_peak_value,all_peak_location]= findpeaks(thigh_orient_y);
[all_trough_value,all_trough_location]= findpeaks(-thigh_orient_y);
all_trough_value = -all_trough_value;
[main_trough_value,main_trough_location]= findpeaks(thigh_orient_y,'MinPeakProminence',30);
for i = 1:length(main_trough_location)
index = all_peak_location == main_trough_location(i);
start_location(counter) = all_trough_location(find(index)-1);
start_value(counter) = all_trough_value(find(index)-1);
for i = 1:length(main_trough_location)
index = all_trough_location > main_trough_location(i);
end_location(counter) = all_trough_location(find(index,1));
end_value(counter) = all_trough_value(find(index,1));
plot(all_trough_location,all_trough_value,'ko')
plot(main_trough_location,main_trough_value,'b*')
plot(start_location,start_value,'r*')
plot(end_location,end_value,'g*')
h = legend('signal',' all troughs','main peak','start', 'end');
set(h,'location','northeast','box','off')
Now the code is apparently picking the right troughs, right?
So the code assumes that the data has its first peak before its first trough. If it's the other way, the indexing is off by one. So that's the source of the problem.
Now, what's the solution? Well, you could check that the first peak is before the first trough, and if it's not, add an appropriate element at the beginning of all_peak_value and all_peak_location (and probably a flag that signifies that a pseudo-peak has been included, so don't plot it). That would probably work ok, but I prefer to re-think the method of finding the relevant troughs for each main peak to make it work in either situation. I think the thing to do is not rely on any relationship between all_peak_location(i) and all_trough_location(i) but make it work regardless of the order of peaks and troughs.
[all_peak_value,all_peak_location]= findpeaks(thigh_orient_y);
[all_trough_value,all_trough_location]= findpeaks(-thigh_orient_y);
all_trough_value = -all_trough_value;
[main_trough_value,main_trough_location]= findpeaks(thigh_orient_y,'MinPeakProminence',30);
for i = 1:length(main_trough_location)
index = find(all_trough_location < main_trough_location(i),1,'last');
start_location(counter) = all_trough_location(index);
start_value(counter) = all_trough_value(index);
for i = 1:length(main_trough_location)
index = find(all_trough_location > main_trough_location(i),1);
end_location(counter) = all_trough_location(index);
end_value(counter) = all_trough_value(index);
plot(all_trough_location,all_trough_value,'ko')
plot(main_trough_location,main_trough_value,'b*')
plot(start_location,start_value,'r*')
plot(end_location,end_value,'g*')
h = legend('signal',' all troughs','main peak','start', 'end');
set(h,'location','northeast','box','off')
That looks good, right? And it still works if I remove the first trough:
thigh_orient_y(1:10) = [];
[all_peak_value,all_peak_location]= findpeaks(thigh_orient_y);
[all_trough_value,all_trough_location]= findpeaks(-thigh_orient_y);
all_trough_value = -all_trough_value;
[main_trough_value,main_trough_location]= findpeaks(thigh_orient_y,'MinPeakProminence',30);
for i = 1:length(main_trough_location)
index = find(all_trough_location < main_trough_location(i),1,'last');
start_location(counter) = all_trough_location(index);
start_value(counter) = all_trough_value(index);
for i = 1:length(main_trough_location)
index = find(all_trough_location > main_trough_location(i),1);
end_location(counter) = all_trough_location(index);
end_value(counter) = all_trough_value(index);
plot(all_trough_location,all_trough_value,'ko')
plot(main_trough_location,main_trough_value,'b*')
plot(start_location,start_value,'r*')
plot(end_location,end_value,'g*')
h = legend('signal',' all troughs','main peak','start', 'end');
set(h,'location','northeast','box','off')