MATLAB Answers

Utilizing Gfortran with Matlab File Output

2 views (last 30 days)
smnitro
smnitro on 29 Jan 2018
Edited: Josh G. on 1 Apr 2019
Hello, I am working to have some Fortran code output data into a .mat format so that we can work on the data in matlab faster than having to write ascii files in Fortran and read them in Matlab.
Matlab seems to be providing a method to do this, however, I am having trouble getting the sample file to run: http://www.mathworks.com/help/matlab/matlab_external/creating-a-mat-file-in-fortran.html I feel the issue has to be in the compiler flags or libraries I need to include, but I am not
I have tried the following command line fortran calls, however those both ran into issues:
gfortran -o matout matlabExample.F
gfortran -cpp -o matout matlabExample.F
gfortran -cpp -o matout matlabExample.F -I/opt/local/matlab/r2017a/bin/glnxa64/mexopts/
They lead to the following error:
/tmp/ccfoBdMw.o: In function `MAIN__':
matlabExample.F:(.text+0x85): undefined reference to `matopen_'
matlabExample.F:(.text+0x17b): undefined reference to `mxcreatedoublematrix730_'
matlabExample.F:(.text+0x18f): undefined reference to `mxgetpr_'
matlabExample.F:(.text+0x1ad): undefined reference to `mxcopyreal8toptr730_'
matlabExample.F:(.text+0x1c6): undefined reference to `mxcreatedoublematrix730_'
matlabExample.F:(.text+0x1dd): undefined reference to `mxcreatestring_'
matlabExample.F:(.text+0x1f4): undefined reference to `mxcreatestring_'
matlabExample.F:(.text+0x216): undefined reference to `matputvariableasglobal_'
matlabExample.F:(.text+0x2ad): undefined reference to `matputvariable_'
matlabExample.F:(.text+0x344): undefined reference to `matputvariable_'
matlabExample.F:(.text+0x3db): undefined reference to `matputvariable_'
matlabExample.F:(.text+0x464): undefined reference to `mxgetpr_'
matlabExample.F:(.text+0x482): undefined reference to `mxcopyreal8toptr730_'
matlabExample.F:(.text+0x4a1): undefined reference to `matputvariable_'
matlabExample.F:(.text+0x534): undefined reference to `matdeletevariable_'
matlabExample.F:(.text+0x5bd): undefined reference to `matclose_'
matlabExample.F:(.text+0x653): undefined reference to `matopen_'
matlabExample.F:(.text+0x6e7): undefined reference to `matgetvariable_'
matlabExample.F:(.text+0x6fb): undefined reference to `mxisfromglobalws_'
matlabExample.F:(.text+0x789): undefined reference to `matgetvariable_'
matlabExample.F:(.text+0x79d): undefined reference to `mxisnumeric_'
matlabExample.F:(.text+0x82b): undefined reference to `matgetvariable_'
matlabExample.F:(.text+0x83f): undefined reference to `mxischar_'
matlabExample.F:(.text+0x8cd): undefined reference to `matgetvariable_'
matlabExample.F:(.text+0x957): undefined reference to `mxdestroyarray_'
matlabExample.F:(.text+0x968): undefined reference to `mxdestroyarray_'
matlabExample.F:(.text+0x979): undefined reference to `mxdestroyarray_'
matlabExample.F:(.text+0x98a): undefined reference to `mxdestroyarray_'
matlabExample.F:(.text+0x99b): undefined reference to `matclose_'
collect2: error: ld returned 1 exit status
Computer: Linux (Ubuntu)
Fortran Compiler: gfortran (Version: 5.4.0 20160609)
Matlab Version: R2017a
Any insight on the issue would help me significantly. Thanks!

Accepted Answer

Josh G.
Josh G. on 1 Apr 2019
Edited: Josh G. on 1 Apr 2019
I know this is pretty late, but since nobody else answered your question, check the output of mex -v $foo.F:
Building with 'gfortran'.
/usr/bin/gfortran -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -I"/usr/local/MATLAB/R2019a/extern/include" -I"/usr/local/MATLAB/R2019a/simulink/include" -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -fdefault-integer-8 -O2 "/usr/local/MATLAB/R2019a/extern/examples/eng_mat/matdemo1.F" -o /tmp/mex_2352360822435_3445/matdemo1.o
f951: Warning: Nonexistent include directory ‘/usr/local/MATLAB/R2019a/simulink/include’ [-Wmissing-include-dirs]
/usr/bin/gfortran -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -I"/usr/local/MATLAB/R2019a/extern/include" -I"/usr/local/MATLAB/R2019a/simulink/include" -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -fdefault-integer-8 -O2 "/usr/local/MATLAB/R2019a/extern/version/fortran_mexapi_version.F" -o /tmp/mex_2352360822435_3445/fortran_mexapi_version.o
f951: Warning: Nonexistent include directory ‘/usr/local/MATLAB/R2019a/simulink/include’ [-Wmissing-include-dirs]
/usr/bin/gfortran -pthread -shared -O -Wl,--version-script,/usr/local/MATLAB/R2019a/extern/lib/glnxa64/fortran_exportsmexfileversion.map /tmp/mex_2352360822435_3445/matdemo1.o /tmp/mex_2352360822435_3445/fortran_mexapi_version.o -Wl,-rpath-link,/usr/local/MATLAB/R2019a/bin/glnxa64 -L"/usr/local/MATLAB/R2019a/bin/glnxa64" -lmx -lmex -lmat -lm -L"/usr/lib64" -lgfortran -o matdemo1.mexa64
MEX completed successfully.
Specifically, to get a mex file to compile successfully, you need to link libraries in $MATLABROOT/bin/$ARCH, in this case taken care of in the third compilation line. There are a few more compiler options required to get the file to run in MATLAB, which is what the other stuff in those commands takes care of.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!