  The genus g free energies of matrix models can be promoted to modular
invariant, non-holomorphic amplitudes which only depend on the geometry of the
classical spectral curve. We show that these non-holomorphic amplitudes satisfy
the holomorphic anomaly equations of Bershadsky, Cecotti, Ooguri and Vafa. We
derive as well holomorphic anomaly equations for the open string sector. These
results provide evidence at all genera for the Dijkgraaf--Vafa conjecture
relating matrix models to type B topological strings on certain local
Calabi--Yau threefolds.
