How to fix Matlab mex error: You can't use 'macro parameter character #' in vertical mode.
Problem
While trying to compile your Matlab or Simulink S-Function using a command such as
mex mysfunction.cpp
you get the following error:
This is pdfTeX, Version 3.141592653-2.6-1.40.26 (TeX Live 2024) (preloaded format=mex)
restricted \write18 enabled.
entering extended mode
(./sine_wave_generator.cpp
This is MeX Version 1.05 18 XII 1993 (B. Jackowski & M. Ry\'cko)
! You can't use `macro parameter character #' in vertical mode.
l.1 #
define S_FUNCTION_NAME sine_wave_generator
?
Solution
The issue here is that you are not using mex
from Matlab, but a different TeX-based program called mex
. This is likely due to the mex
binary being in your PATH
environment variable before the Matlab binary directory.
To fix this, you can either:
- Use the full path to the
mex
binary in your Matlab installation, e.g./usr/local/MATLAB/R2021b/bin/mex
, or - Change the order of directories in your
PATH
environment variable so that the Matlab binary directory comes before the othermex
binary directory. - Use the
mex
command from the Matlab command line, which should automatically use the correct binary.