简体   繁体   中英

Importing .mat file in Fortran - Segmentation Fault error

I am writing this F90 program to compute a function in fortran which takes the input from a .mat file and save the results in another .mat file.

I followed this answer to get the code compiled and correctly linked. This is my makefile command:

gfortran -g -fcheck=all binhkorn_mat.F90 -I/usr/local/MATLAB/R2015b/extern/include/ -L/usr/local/MATLAB/R2015b/bin/glnxa64 -cpp -o binhkorn_mat -lmat -lmx -Wl,-rpath /usr/local/MATLAB/R2015b/bin/glnxa64/

The output file is apparently correctly compiled, but then once I run the program the following SF appears (I'm working on LINUX Ubuntu 14.04 LTS):

    Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7FD650063F27
#1  0x7FD6500644F4
#2  0x7FD64FCBCD3F
#3  0x7FD64FDD7AF6
#4  0x400A3F in binhkorn_mat at binhkorn_mat.F90:17 (discriminator 2)
 ./binhkorn_mat: Segmentation fault

I can't figure out if there's an error with the compiler or if I did something wrong with the pointers/functions definitions. Here's the code (binhkorn_mat.F90):

 #include "fintrf.h"
PROGRAM binhkorn_mat
IMPLICIT NONE
mwPointer matOpen, matGetVariable, matPutVariable
mwPointer mpin, mpX, mpout, mpcf
INTEGER :: i,j
REAL*8, DIMENSION(2) :: x
REAL*8, DIMENSION(4) :: cf

!input/output through .mat f
mpin = matOpen('X.mat', 'u')
mpX = matGetVariable(mpin, 'X')
CALL mxCopyPtrToReal8(mpX, x, 2)
CALL matClose(mpin)

!fitness functions
 cf(1) = ((x(1)-2)**2 + (x(2)-1)**2 + 2)
 cf(2) = (9*x(1) + (x(2)-1)**2)

!constraints
 cf(3) = x(1)*x(1) + x(2)*x(2) - 225
 cf(4) = x(1) - 3*x(2) + 10

!output file created
CALL mxCopyReal8ToPtr(cf, mpcf, 4)
mpout = matOpen('cf.mat', 'w')
mpcf = matPutVariable(mpout, 'cf', mpcf)
CALL matClose(mpout)

END PROGRAM

The X.mat file is correctly created by an external Matlab script and contains a variable named X which is a 2-element row vector.

I basically misunderstood how to use of many functions. The pointers i supplied as input to some of them were not the correct ones. I post here the working solution:

#include "fintrf.h"
PROGRAM binhkorn_mat
IMPLICIT NONE
mwPointer matOpen, matGetVariable!, matPutVariable
mwPointer mxGetData, mxGetNumberOfElements, mxCreateNumericArray
mwPointer mpin, mpX, mpout, mpcf
mwSize ndim
mwSize dims(2)
INTEGER :: s
INTEGER*4 mxClassIDFromClassName
 CHARACTER (LEN = 6) :: classname
REAL*8, DIMENSION(2) :: x
REAL*8, DIMENSION(4) :: cf

!input/output through .mat f
mpin = matOpen('X.mat', 'r')
mpX = matGetVariable(mpin, 'X')
CALL mxCopyPtrToReal8(mxGetData(mpX), x, mxGetNumberOfElements(mpX))
!CALL matClose(mpin)

!fitness functions
 cf(1) = ((x(1)-2)**2 + (x(2)-1)**2 + 2)
 cf(2) = (9*x(1) + (x(2)-1)**2)

!constraints
 cf(3) = x(1)*x(1) + x(2)*x(2) - 225
 cf(4) = x(1) - 3*x(2) + 10

!output .mat file created and filled
s = size(cf)
ndim = 2
 classname = 'double'
dims(1) = 1
dims(2) = s
mpcf = mxCreateNumericArray(ndim, dims, mxClassIDFromClassName(classname), 0)
CALL mxCopyReal8ToPtr(cf, mxGetData(mpcf), mxGetNumberOfElements(mpcf))
mpout = matOpen('cf.mat', 'w')
CALL matPutVariable(mpout, 'cf', mpcf)
!CALL matClose(mpout)

END PROGRAM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM