Identify the frequency spectrum of an audio file by using matlab
It looks like you are trying to recreate the functionality in spectrogram. Spectrogram will give you a short-time Fourier transform using whatever blocksize you specify.

Much of your code looks like it is re-inventing this.