Beginner's Mex Error - MatLab

8 views (last 30 days)
Marcelo Sena
Marcelo Sena on 2 Feb 2015
Answered: Quanshangze DU on 22 Nov 2019
I am beginning to use the mex setup to call fortran files from matlab. What I am trying to do is call a fortran subroutine inside an m-file. From what I've read, I need to run:
mex filename.f90
However, when I do that, I get lots of error messages of the type:
error #5149: Illegal
character in statement label field [s]
However, I am using a .f90 file that is supposedly correct (I got it from another source). Here are the first beginning lines of the fortran file:
subroutine mexFunction(nlhs,plhs,nrhs,prhs)
!--------------------------------------------------------------------
! Matlab gateway for sirff
implicit none
! pointers to input/output data
! always take INTEGER*8, to let it work on 64-bit machines (SGI e.g.)
! 32-bit compilers will correct this to INTEGER*4, so don't worry about the
! warning(s) on this during compilation.
integer(8) :: plhs(*), prhs(*)
I am a beginner to this, so bear with me please. Any help is appreciated.
Thanks.

Answers (2)

James Tursa
James Tursa on 2 Feb 2015
Edited: James Tursa on 2 Feb 2015
Check the associated mexopts file for the specific compiler/linker setup you are using and look for the COMPFLAGS line. If there is a "/fixed" option on that line, delete it! I have no idea why TMW continues this practice of forcing the compiler into fixed form mode when it should be left to the file extension (.f or .f90) to determine fixed or free format. E.g.,
Look in the following folder (or similar folder e.g. win64)
[matlabroot '\bin\win32\mexopts']
Older version of MATLAB use .bat files, newer versions of MATLAB use .xml files. Find the file name that is associated with your compiler. E.g., on my system there is the following file:
intel_fortran_13_v2012.xml
and when you open that file there is the following entry:
COMPFLAGS="/nologo /fpp /Qprec /fixed /MD /fp:source /assume:bscc $COMPDEFINES $INCLUDE"
The /fixed option shown is the likely problem. Get rid of it! I.e., edit the file and remove the /fixed text so the line reads:
COMPFLAGS="/nologo /fpp /Qprec /MD /fp:source /assume:bscc $COMPDEFINES $INCLUDE"
As another example, on an older version of MATLAB there is a .bat file, e.g.,
intelf12msvs2010opts.bat
And you will see the same "/fixed" problem on the COMPFLAGS line in that file. Get rid of it.
Finally, before you edit any of these files I would advise copying the entire directory to another location so that you can recover to the original files if necessary.
SIDE COMMENT:
I have no idea what the comment in the code "32-bit compilers will correct this to INTEGER*4" means ... I am unaware of any such "automatic" compiler behavior.
  6 Comments
James Tursa
James Tursa on 3 Feb 2015
Can you run "mex filename.f90 -v" to verify what settings are actually being used?
Marcelo Sena
Marcelo Sena on 4 Feb 2015
Edited: Marcelo Sena on 4 Feb 2015
It was using a compiler not located at the mexopts folder. I openedthe xml file, removed the /fixed and it now works! Thank you very much. Here is the output of mex filename.f90 -v, just in case you spot something weird...
Verbose mode is on.
Neither -compatibleArrayDims nor -largeArrayDims is selected.
Using -compatibleArrayDims. In the future, MATLAB will require the use of
-largeArrayDims and remove the -compatibleArrayDims option.
For more information:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
... Looking for compiler 'Intel Visual Fortran Composer XE 2013 with Microsoft Visual Studio 2008' ...
... Looking for environment variable 'IFORT_COMPILER14' ...No.
... Looking for environment variable 'IFORT_COMPILER13' ...Yes ('C:\Program Files (x86)\Intel\Composer XE 2013\').
... Looking for file 'C:\Program Files (x86)\Intel\Composer XE 2013\Bin\intel64\ifort.exe' ...Yes.
... Looking for folder 'C:\Program Files (x86)\Intel\Composer XE 2013' ...Yes.
... Looking for registry setting 'HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1' InstallationFolder ...Yes ('C:\Program Files\Microsoft SDKs\Windows\v7.1\').
... Looking for registry setting 'HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VC7' 10.0 ...Yes ('C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\').
... Looking for file 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\cl.exe' ...Yes.
... Looking for folder 'C:\Program Files (x86)\Microsoft Visual Studio 10.0' ...Yes.
Found installed compiler 'Intel Visual Fortran Composer XE 2013 with Microsoft Visual Studio 2008'.
Set PATH = C:\Program Files (x86)\Intel\Composer XE 2013\bin\intel64;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Bin\amd64;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Bin\VCPackages;C:\Program Files\Microsoft SDKs\Windows\v7.1\\Bin\x64;C:\Program Files\Microsoft SDKs\Windows\v7.1\\Bin;;C:\Program Files (x86)\Intel\Trace Analyzer and Collector\9.0.1.035\bin;C:\Program Files (x86)\Intel\MPI\5.0.1.037\intel64\bin;C:\Program Files (x86)\Intel\Trace Analyzer and Collector\9.0.2.045\bin;C:\Program Files (x86)\Intel\MPI\5.0.2.044\intel64\bin;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\mpirt;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32\mpirt;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32\compiler;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\libnvvp;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Program Files\MATLAB\R2014b\runtime\win64;C:\Program Files\MATLAB\R2014b\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft Windows Performance Toolkit\;C:\Program Files (x86)\Intel\Trace Analyzer and Collector\9.0.1.035\dll;C:\Program Files (x86)\Intel\Trace Analyzer and Collector\9.0.2.045\dll
Set INCLUDE = C:\Program Files (x86)\Intel\Composer XE 2013\include;C:\Program Files (x86)\Intel\Composer XE 2013\compiler\include;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE;C:\Program Files\Microsoft SDKs\Windows\v7.1\\INCLUDE;C:\Program Files\Microsoft SDKs\Windows\v7.1\\INCLUDE\gl;C:\Program Files\MATLAB\R2014b\extern\include;;C:\Program Files (x86)\Intel\Trace Analyzer and Collector\9.0.1.035\include;C:\Program Files (x86)\Intel\Trace Analyzer and Collector\9.0.2.045\include
Set LIB = C:\Program Files (x86)\Intel\Composer XE 2013\lib\intel64;C:\Program Files (x86)\Intel\Composer XE 2013\compiler\lib\intel64;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Lib\amd64;C:\Program Files\Microsoft SDKs\Windows\v7.1\\Lib\X64;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\ATLMFC\LIB\AMD64;C:\Program Files\MATLAB\R2014b\lib\win64;C:\Program Files\MATLAB\R2014b\extern\lib\win64;
Set LIBPATH = C:\Program Files (x86)\Intel\Composer XE 2013\compiler\Lib\Intel64;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Lib\amd64;C:\Program Files\Microsoft SDKs\Windows\v7.1\\LIB\X64;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\ATLMFC\LIB\AMD64;C:\Program Files\MATLAB\R2014b\extern\lib\win64;
Options file details
-------------------------------------------------------------------
Compiler location: C:\Program Files (x86)\Intel\Composer XE 2013
Options file: C:\Users\Marcelo\AppData\Roaming\MathWorks\MATLAB\R2014b\mex_FORTRAN_win64.xml
CMDLINE100 : ifort /c /nologo /fpp /Qprec /fixed /MD /fp:source /assume:bscc -I"C:\Program Files\MATLAB\R2014b\extern\include" /DMX_COMPAT_32 /DMATLAB_MEX_FILE /O2 /DNDEBUG "C:\Users\..." /FoC:\Users\Marcelo\AppData\Local\Temp\mex_247517124086464_10164\sirff.obj
CMDLINE200 : link /nologo /manifest kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /incremental:NO /DLL /EXPORT:MEXFUNCTION C:\Users\Marcelo\AppData\Local\Temp\mex_247517124086464_10164\sirff.obj /LIBPATH:"C:\Program Files\MATLAB\R2014b\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /out:sirff.mexw64 /MAP:"sirff.map"
CMDLINE250 : mt -outputresource:sirff.mexw64;2 -manifest sirff.mexw64.manifest
CMDLINE300 : del sirff.exp sirff.lib "sirff.map" sirff.mexw64.manifest sirff.ilk
COMPILER : ifort
COMPFLAGS : /nologo /fpp /Qprec /fixed /MD /fp:source /assume:bscc -I"C:\Program Files\MATLAB\R2014b\extern\include" /DMX_COMPAT_32 /DMATLAB_MEX_FILE
COMPDEFINES : /DMX_COMPAT_32 /DMATLAB_MEX_FILE
MATLABMEX : /DMATLAB_MEX_FILE
OPTIMFLAGS : /O2 /DNDEBUG
INCLUDE : -I"C:\Program Files\MATLAB\R2014b\extern\include"
DEBUGFLAGS : /Z7
LINKER : link
LINKFLAGS : /nologo /manifest kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /incremental:NO
LINKTYPE : /DLL
LINKEXPORT : /EXPORT:MEXFUNCTION
LINKLIBS : /LIBPATH:"C:\Program Files\MATLAB\R2014b\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib
LINKDEBUGFLAGS : /debug /PDB:"sirff.mexw64.pdb"
LINKOPTIMFLAGS :
OBJEXT : .obj
LDEXT : .mexw64
SETENV : set COMPILER=ifort
set COMPFLAGS=/c /nologo /fpp /Qprec /fixed /MD /fp:source /assume:bscc -I"C:\Program Files\MATLAB\R2014b\extern\include" /DMX_COMPAT_32 /DMATLAB_MEX_FILE /DMX_COMPAT_32 /DMATLAB_MEX_FILE /DMATLAB_MEX_FILE
set OPTIMFLAGS=/O2 /DNDEBUG
set DEBUGFLAGS=/Z7
set LINKER=link
set LINKFLAGS=/nologo /manifest kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /incremental:NO /export:%ENTRYPOINT% /DLL /LIBPATH:"C:\Program Files\MATLAB\R2014b\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /EXPORT:MEXFUNCTION
set LINKDEBUGFLAGS=/debug /PDB:"%OUTDIR%%MEX_NAME%.mexw64.pdb"
set NAME_OUTPUT=/out:"%OUTDIR%%MEX_NAME%%MEX_EXT%"
FORTRANROOT : C:\Program Files (x86)\Intel\Composer XE 2013
SDKROOT : C:\Program Files\Microsoft SDKs\Windows\v7.1\
VSROOT : C:\Program Files (x86)\Microsoft Visual Studio 10.0
MATLABROOT : C:\Program Files\MATLAB\R2014b
ARCH : win64
SRC : "C:\Users..."
OBJ : C:\Users\Marcelo\AppData\Local\Temp\mex_247517124086464_10164\sirff.obj
OBJS : C:\Users\Marcelo\AppData\Local\Temp\mex_247517124086464_10164\sirff.obj
SRCROOT : C:\Users\Marcelo\Desktop\Marcelo\PUC\Mestrado\Paper de Verão\Rotinas\Eu\Neoclassical without Labor a la Econo Reviews\likelihood cut\sirff
DEF : C:\Users\Marcelo\AppData\Local\Temp\mex_247517124086464_10164\sirff.def
EXP : sirff.exp
LIB : sirff.lib
EXE : sirff.mexw64
ILK : sirff.ilk
MANIFEST : sirff.mexw64.manifest
TEMPNAME : sirff
EXEDIR :
EXENAME : sirff
OPTIM : /O2 /DNDEBUG
LINKOPTIM :
-------------------------------------------------------------------
Building with 'Intel Visual Fortran Composer XE 2013 with Microsoft Visual Studio 2008'.
ifort /c /nologo /fpp /Qprec /fixed /MD /fp:source /assume:bscc -I"C:\Program Files\MATLAB\R2014b\extern\include" /DMX_COMPAT_32 /DMATLAB_MEX_FILE /O2 /DNDEBUG "C:\Users\Marcelo\..." /FoC:\Users\Marcelo\AppData\Local\Temp\mex_247517124086464_10164\sirff.obj

Sign in to comment.


Quanshangze DU
Quanshangze DU on 22 Nov 2019
As a complement from the year 2019 ==>
I use MATLAB R2019a on Win10 and the 'mex' function, by the command 'mex -setup', is configured as "MEX configured to use 'Intel Parallel Studio XE 2015 for Fortran with Microsoft Visual Studio 2015' for FORTRAN language compilation."
I met the same problem as presented above because of the 'fixed format' by default. I tried to remove '/fixed' from the file in MATLABroot bin\win64\mexopts. As you may see, there are multiple files of configuration for Fortran, c and cpp, like intel_fortran_xx_vsxxxx.xml, where xx stands for 15/16/17.. and xxxx stand for 2015/17.... These files correspond to, I suppose, the configuration your matlab's mex function takes. I tried to modify the file of my case, namely 'intel_fortran_15_vs2015.xml'. But nothing has changed. Still the problem coming from fixed format. So I modifed all the xml files because of uncertainty.. It turned out meaingless.
So the file to modify is not in the folder '\bin\win64\mexopts', but in '\user\AppData\Roaming\MathWorks\Matlab\R2019a'. Just remove the term '\fixed' in the file 'mex_FORTRAN_win64.xml' and then it will work.
Good luck !
qdu

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!