Matrix influence on the optical spectra of isolated Mg and Ca atoms