diff --git a/README.md b/README.md
index 6d712a88846fdd80c50d2e703ae9b01ca15f8c7a..026f321ab9fa7ea87a3087d95f20490abffeb5e0 100644
--- a/README.md
+++ b/README.md
@@ -114,10 +114,15 @@ Alternatively, you can disable the compilation of MEX files for MATLAB with the
 
 You may need to specify additional options to the configure script, see the platform specific instructions below.
 
-Note that if you don't want to compile with debugging information, you can specify the `CFLAGS` and `CXXFLAGS` variables to configure, such as:
+Note that if you don't want to compile the C/C++ programs with debugging information, you can specify the `CFLAGS` and `CXXFLAGS` variables to the configure script, such as:
 ```
 ./configure CFLAGS="-O3" CXXFLAGS="-O3"
 ```
+To remove debugging information for Matlab mex functions, the analagous call would be:
+```
+./configure MATLAB_MEX_CFLAGS="-O3" MATLAB_MEX_CXXFLAGS="-O3"
+```
+
 If you want to give a try to the parallelized versions of some mex files (`A_times_B_kronecker_C` and `sparse_hessian_times_B_kronecker_C` used to get the reduced form of the second order approximation of the model) you can add the `--enable-openmp` flag, for instance:
 ```
 ./configure --with-matlab=/usr/local/matlab78 MATLAB_VERSION=7.8 --enable-openmp
diff --git a/m4/ax_mexopts.m4 b/m4/ax_mexopts.m4
index c245e2511a3ae19a55203ccc901bb87b52622f98..11350a655adc610249231cd3157db9c75efec10f 100644
--- a/m4/ax_mexopts.m4
+++ b/m4/ax_mexopts.m4
@@ -98,6 +98,32 @@ else
   AC_MSG_RESULT([unknown])
 fi
 
+# Allow user to override default Matlab compilation flags
+# Github ticket #1121
+if test "x$MATLAB_MEX_CPPFLAGS" != "x"; then
+  MATLAB_CPPFLAGS="$MATLAB_CPPFLAGS $MATLAB_MEX_CPPFLAGS"
+fi
+
+if test "x$MATLAB_MEX_DEFS" != "x"; then
+  MATLAB_DEFS="$MATLAB_DEFS $MATLAB_MEX_DEFS"
+fi
+
+if test "x$MATLAB_MEX_CFLAGS" != "x"; then
+  MATLAB_CFLAGS="$MATLAB_CFLAGS $MATLAB_MEX_CFLAGS"
+fi
+
+if test "x$MATLAB_MEX_CXXFLAGS" != "x"; then
+  MATLAB_CXXFLAGS="$MATLAB_CXXFLAGS $MATLAB_MEX_CXXFLAGS"
+fi
+
+if test "x$MATLAB_MEX_LDFLAGS" != "x"; then
+  MATLAB_LDFLAGS="$MATLAB_LDFLAGS $MATLAB_MEX_LDFLAGS"
+fi
+
+if test "x$MATLAB_MEX_LIBS" != "x"; then
+  MATLAB_LIBS="$MATLAB_LIBS $MATLAB_MEX_LIBS"
+fi
+
 AC_SUBST([MATLAB_CPPFLAGS])
 AC_SUBST([MATLAB_DEFS])
 AC_SUBST([MATLAB_CFLAGS])