How to use MATLAB's $\tt xcorr$ to find index of a section inside a whole recording? Trying to find the index offset of a short section of an audio I have taken out of my recording with cross-correlation, but this method sometimes succeeds finding the index of the short section sometimes fails, seems that it depends which sections are being compared and the length of the section made.

My question is what makes the xcorr lose its accuracy ? And if is it dependable ?

clear all; 
close all;

[A,fs]=audioread('hendrix.wav'); % some random audio

offset=36644-(784/2); % here I define the offset for my selection.

B=A(offset:offset+784); %here I make selection from A 784 samples long...

[c,lags] = xcorr(A,B);  % c is the correlation, should have a clear peak

c=c((round(length(c)/2):round(length(c)))); %positive coefficients                               

[ff,ind]=max(c); % finding the index of the offset

M=A(ind:ind+(length(B))-1); % the selection from A based on the new index

plot(B);
hold;
plot(M);
Tried with taking absolute value of the maximum ? I have updated the code, see if that works for you... 

[ff,ind]=max(abs(c)); % finding the index of the offset

M=A(ind:ind+(length(B))-1); % the selection from A based on the new index

plot(B);
hold;
plot(M);