Regarding to calling Matlab from Fortran program (fengdemo.f, matdemo1.f)
Show older comments
I am using matlab R2011b on a labtop with windows 7- 64 bit and a compiler, Intel Visual Fortran 11.1.
I am learning about how to call the Matlab from a Fortran program by using fengdemo.f, matdemo1.f. There is no problem in building the program, but during execution, after passing the line with mxCreateDoubleMatrix, the following error was shown:
forrt1: severe(157): Program Exception -access violation
Can anyone help me to solve my problem. Thank you!
1 Comment
tong wei
on 28 Aug 2013
I have got this trouble too. When I changed all the integer to integer*8, the problem is solved.
Answers (1)
James Tursa
on 15 Apr 2013
Edited: James Tursa
on 15 Apr 2013
For the API function interfaces, it is quite possible that the integers involved need to be 8-byte integers instead of 4-byte integers ... depends on system involved and compile options. E.g., for the fengdemo.f program I see the following line:
T = mxCreateDoubleMatrix(1, 10, 0)
This is not a robust way to code this call and may only work on 32-bit systems. You can get away with stuff like this in a C/C++ program when the arguments are passed-by-value and automatically promoted to the correct type by the compiler, but you can't get away with it in a Fortran program where the arguments are passed by reference. In the Fortran case the arguments must match exactly in type. So I would advise the following:
1) Copy the fengdemo.f file to a new file, e.g. fengdemo64.f
2) Edit the fengdemo64.f file and change all hard-coded argument constants to variables that match the API in signature. E.g., the signature for mxCreateDoubleMatrix is:
mwPointer mxCreateDoubleMatrix(m, n, ComplexFlag)
mwSize m, n
integer*4 ComplexFlag
So instead of passing constants for the arguments, do this instead:
mwSize m, n
integer*4 ComplexFlag
:
m = 1
n = 10
ComplexFlag = 0
:
T = mxCreateDoubleMatrix(m, n, ComplexFlag)
3) Do this for all of the API calls that have constant arguments. I.e., change the following lines per the above example as well:
call mxCopyReal8ToPtr(time, mxGetPr(T), 10)
call mxCopyPtrToReal8(mxGetPr(D), dist, 10)
4) Do the same thing for all the Fortran example code.
5) If you are up to it, also edit the appropriate mexopts.bat file for your compiler and remove the /fixed option from the COMPFLAGS line. This silly option will prevent you from compiling F90/95 etc free format code, so it is best to remove it.
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!