In this paper we present an extension of the modal method by Gegenbauer expansion (MMGE) [J. Opt. Soc. Am. A28, 2006 (2011)], [Progress Electromagn. Res.133, 17 (2013)] to the study of nonperiodic problems. The nonperiodicity is introduced through the perfectly matched layers (PMLs) concept, which can be introduced in an equivalent way either by a change of coordinates or by the use of a uniaxial anisotropic medium. These PMLs can generate strong irregularities of the electromagnetic fields that can significantly alter the convergence and stability of the numerical scheme. This is the case, e.g., for the famous Fourier modal method, especially when using complex stretching coordinates. In this work, it will be shown that the MMGE equipped with PMLs is a robust approach because of its natural immunity against spurious modes.