Before moving on to what the MATLAB command does, you probably want to know what a spectrogram is. This way you will get more information about how each parameter works.
The spectrogram is a visual representation of the Short-Term Fourier Transform . Think about it by taking pieces of the input signal and applying the local Fourier transform on each fragment. Each piece has a given width, and you apply the Fourier transform to this fragment. You must take into account that each piece has an associated frequency distribution. For each fragment that is centered at a particular point in time in your time signal, you get a bunch of frequency components. Collecting all these frequency components on each piece and building everything together is, in fact, a spectrogram.
The spectrogram is a 2D visual map of heat where the horizontal axis represents the signal time and the vertical axis represents the frequency axis. What is being rendered is an image in which darker colors mean that for a given point in time and a specific frequency, the lower the frequency components of the frequency, the darker the color. Similarly, the larger the frequency components, the brighter the color.
Here is one great example of a spectrogram:

Source: Wikipedia
Therefore, for each time point, we see the distribution of frequency components. Think of each column as the frequency decomposition of a piece concentrated at that point in time. For each column, we see a different spectrum of colors. The darker the color, the smaller the magnitude component at that frequency and vice versa.
So! ... now you are armed with this, let go of how MATLAB works in terms of a function and its parameters. The way you call spectrogram corresponds to this version of the function:
spectrogram(x,window,noverlap,nfft,fs)
Skip each parameter one at a time so you can better understand what each does:
x is the input signal in the time domain that you want to find the spectrogram. It could not be much easier. In your case, the signal you want to find the spectrogram is defined in the following code:
N=5000; phi = (rand(1,N)-0.5)*pi; a = tan((0.5.*phi)); i = 2.*a./(1-a.^2);
Here i is the signal you want to find the spectrogram.
window - If you remember, we will decompose the image into pieces, and each piece has a specified width. window defines the width of each fragment in terms of patterns . Since this is a discrete time signal, you know that this signal was selected with a specific sampling frequency and sampling period. You can determine how large the window is in terms of patterns:
window_samples = window_time/Ts
Ts is the sampling time of your signal. Setting the window size is actually very empirical and requires a lot of experimentation. Basically, the larger the window, the better the frequency resolution that you get when you capture more frequencies, but the localization of time leaves much to be desired. Similarly, the smaller the window size, the better the localization you have in time, but you will not get such a big difference in frequency. I have no suggestions here about what is the most optimal size ... that is why wavelets are preferred when it comes to time - frequency decomposition. For each "piece", the pieces are decomposed into smaller pieces of dynamic width, so you get a mixture of good localization of time and frequency.
noverlap - Another way to ensure good frequency localization is that the pieces overlap . A correct spectrogram ensures that each piece has a certain number of samples that overlap for each fragment, and noverlap determines how many samples overlap in each window. The default value is 50% of the width of each fragment.
nfft - You essentially do an FFT of each piece. nfft tells you how many FFT points you need to calculate per piece. The default number of points is the largest of 256, or floor(log2(N)) , where N is the signal length. nfft also provides an estimate of how fine a frequency resolution will be. A higher number of FFT points will give a higher frequency resolution and thus show fine-grained details along the frequency axis of the spectrogram.
fs is the sampling rate of your signal. The default value is 1 Hz, but you can override this regardless of the sample rate of your signal.
Therefore, you should probably learn from this that I cannot tell you how to set the parameters. It all depends on what kind of signal you have, but hopefully the above explanation will give you a better idea of how to set the parameters.
Good luck