Downsampling in MATLAB - why scaling filter coefficients? Trying to undertsand how MATLAB does resampling, looking at toolbox/signal/resample.m. 

This is the part where (downsampling) filter coefficients are calculated:

  fc = 1/2/pqmax;
  L = 2*N*pqmax + 1;
  h = firls( L-1, [0 2*fc 2*fc 1], [1 1 0 0]).*kaiser(L,bta)' ;
  h = p*h/sum(h);


(In this case pqmax and p both represent downsampling ratio.)

Not sure what is the purpose of last line where all filter coefficients are scaled by downsampling ratio over coeffs sum, p/sum(h)? Does anyone know the theory behind? 

MATLAB firls
MATLAB kaiser
firls theory: Least Squared Error Design of FIR Filters
This is done to achieve unity gain, i.e. to have the same energy in the downsampled and original signal.

The sum of filter coefficients sum(h) is the filter gain, by dividing the filter by it, it is set to 1 (unity). 
It is multiplied by p to compensate for the missing samples, again to achieve the same engery as in the original signal.