Detecting a Signal Inside a Time Series Using MATLAB's $ \tt xcorr() $ I would like to find the exact replica of signal A in time series B. I am working with the simple toy example below, in which A is located starting at the 4th position in B.

I thought that using xcorr would achieve this but I am clearly failing somewhere. xcorr doesn't seem to be the right function despite the MATLAB help notes on xcorr and comments such as: 


  Cross-correlation is usually the simplest way to determine the time lag between two signals. The position of peak value indicates the time offset at which the two signals are the most similar.


A = [1 2 1];
B = [0 9 8 1 2 1 7 3 1 0];  
[c, lags] = xcorr(A,B)
lags(c==max(c))
c =
Columns 1 through 12
  0.0000    1.0000    5.0000   14.0000   18.0000   11.0000    6.0000   12.0000   26.0000   26.0000    9.0000    0.0000
Columns 13 through 19
  0.0000    0.0000    0.0000   -0.0000   -0.0000   -0.0000   -0.0000
lags =
  -9    -8    -7    -6    -5    -4    -3    -2    -1     0     1     2     3     4     5     6     7     8     9
ans =
  -1     0


The max correlation values are located at lags of -1 and 0, which doesn't match the offset required to get A to match B. This leads me to ask: 


When is xcorr useful for finding the exact signal in another signal? 2. And what function should I be using to find the location of A in B? 


Ideally I would like to test this method on more complicated signals as well as look for methods of detecting "similar" signals inside other (longer) signals.
The function xcorr calculates the correlation of 2 signals.
The correlation is known to be a good (The MLE) for delay estimation under Gaussian Noise.  

Yet, as can be seen in your data you're not using it in the cases it meant to be used.
If we assume you have a model of a known signal with Additive White Gaussian Noise (AWGN or any other Additive White Noise), than the SNR is so terrible you can not expect it perform well.

If your signal model is additive deterministic signal (The signal [0 9 8 0 0 0 7 3 1 0] in that example) then you should look for a different approach (Though xcorr can be part of it).

Any tool you use, you must be aware for the model it was designed for.