I set window length as 512 to see more frequencies and overlap on 256
(as default), is this correct?
No, the number of frequencies (i.e. frequency bins) will depend on the NFFT argument of your spectrogram function and will be equal to $NFFT / 2$. Also note that the frequency bins do not represent a single frequency component but rather have a fixed bandwidth given by $f_s/NFFT$ ($f_s$ is the sampling rate).
But, there's also one big problem with your code. NFFT, as used in your code, represents the number of samples in your entire audio file (rounded to the next power of two integer), which means that you're going to average all the frequency components found at different time points. A spectrogram is really about segmenting your audio file into smaller time chunks and then performing an FFT on it. So just replace your spectrogram function with
spectrogram(y,512,256, 512);//the last argument is the FFT length
The NFFT and window length arguments can be the same length. The longer the NFFT argument, the more frequency components you'll get at the expense of decreased time resolution (more samples are spread over a larger time segment, thus less time resolution)