Im very new to matlab and medical imaging in general. For a project I need to finish a script that uses two matrices the "kernel" and "searchArea" to determine the displacement of the interventricular septum of the heart using the blockchaining method with multiple frames of the cardiac cycle. The "kernel" is a fraction of the orginal frame image and the "searchArea" is an area thats surrounds the kernel in the next frame. I have to use cross correlation to do this and already tried the following (this process will be repeated for all the frames):
crossCor = xcorr2(searchArea,kernel);
[max_cc,imax] = max(abs(crossCor(:)));
[ypeak, xpeak] = ind2sub(size(crossCor),imax(1));
dispLat = ypeak-size(kernel,1)
dispAx = xpeak-size(kernel,2)
This code was writen based on the xcorr2 documentation: https://nl.mathworks.com/help/signal/ref/xcorr2.html
The code does work but the results do not meet my expectations, the script that I got provided with uses the lateral displacement (x displacement) and axial displacement (y displacement) to create tracking frames:

The results should show the displacement of the septum just like in frame 1, but you can see that in frame 2 and 3 the displacement is way off.
I do not know if I'm on the right track here of giving the provided script the right displacements, but any advice or nudges in the right direction are welcome!
REMARK: I am doing this for an introductory medical imaging course, the blue colored mesh which is used to plot the movement is created using speckle tracking, however I did not get any details on how this works which is probably intended. The Matlab script relies on the right input of axial (y) and lateral (x) displacement, the results are wrong, so my written code is probably (partly) wrong. My main intension to post this on stackoverflow is to receive feedback on my written code, because I don't know what I have done wrong.