Matlab engine in Fortran code (gfortran problem)
3 views (last 30 days)
Show older comments
Hi,
I followed the instructions http://www.mathworks.nl/help/techdoc/matlab_external/f38569.html to interface some matlab models in my fortran code. When compiling the project with Intel Fortran compiler I use:
-fpp -I/usr/local/MATLAB/R2011b/extern/include -I/usr/local/MATLAB/R2011b/simulink/include -fexceptions -DMX_COMPAT_32
for compiling and:
-Wl,-rpath-link,/usr/local/MATLAB/R2011b/bin/glnxa64 -L/usr/local/MATLAB/R2011b/bin/glnxa64 -leng -lmx -lm
for linking and everything works fine!
When I compile with gfortran, I use the same commands (except -fpp which becomes -cpp) and I get a segmentation fault on the last line of:
mwPointer ep, mVx0
double precision Vx0
Vx0=1.d0
ep = engOpen('matlab ')
if (ep .eq. 0) then
write(log,*) 'Can''t start MATLAB engine'
stop
endif
mVx0 = mxCreateDoubleMatrix(1, 1, 0)
call mxCopyReal8ToPtr(Vx0, mxGetPr(mVx0), 1)
Anyone can help out?
Petros
0 Comments
Answers (1)
James Tursa
on 27 Apr 2012
In Fortran you should never pass literal constant arguments to the API functions since they can vary in size among different platforms. You can get away with this in C/C++ because arguments are passed by value and are automatically converted to the correct type, but you can't get away with this practice in Fortran. Instead of doing this:
mVx0 = mxCreateDoubleMatrix(1, 1, 0)
call mxCopyReal8ToPtr(Vx0, mxGetPr(mVx0), 1)
you should be doing this:
mwPointer, external :: mxCreateDoubleMatrix, mxGetPr
mwSize :: one = 1
integer*4 :: complexity = 0
:
mVx0 = mxCreateDoubleMatrix( one, one, complexity )
call mxCopyReal8ToPtr( Vx0, mxGetPr(mVx0), one )
That way the integer arguments should always be the correct type regardless of platform.
You might also be interested in this Fortran 95 interface package:
0 Comments
See Also
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!