필터 지우기
필터 지우기

Writing mat file from fortran

조회 수: 2 (최근 30일)
Leos Pohl
Leos Pohl 2022년 3월 4일
편집: James Tursa 2022년 3월 8일
I am trying to save an array generated in Fortran to mat file so that i can read it in matlab. I am using gfortran to compile the code.
Basically I have in fortran code (it is dirty based on matdemo1.F) is pasted below. The error during compile is:
/usr/bin/ld: /tmp/ccZi95JS.o: in function `MAIN__':
matdemo.F90:(.text+0x89): undefined reference to `matopen800_'
/usr/bin/ld: matdemo.F90:(.text+0x19c): undefined reference to `mxgetdoubles800_'
/usr/bin/ld: matdemo.F90:(.text+0x1c3): undefined reference to `mxcopyreal8toptr800_'
/usr/bin/ld: matdemo.F90:(.text+0x1e5): undefined reference to `matputvariableasglobal800_'
/usr/bin/ld: matdemo.F90:(.text+0x276): undefined reference to `matclose800_'
/usr/bin/ld: matdemo.F90:(.text+0x307): undefined reference to `mxdestroyarray800_'
collect2: error: ld returned 1 exit status
Which i guess is cause by some missing links but I cannot find anywhere any working example. Only mentions about some 'MEX' code which however seems to have to be compiled by Matlab. I want to compile my code in gfortran, not matlab, I just need to save mat files. Is there some useful existing code that can be reused?
program matdemo1
! Declarations
implicit none
mwPointer matOpen, mxCreateNumericArray
#if MX_HAS_INTERLEAVED_COMPLEX
mwPointer mxGetDoubles
#else
mwPointer mxGetPr
#endif
mwPointer mp, pa
integer status, matClose, mxIsFromGlobalWS
integer matPutVariable, matPutVariableAsGlobal
integer*4 mxIsNumeric, mxIsChar
integer*4 classid
integer*4 mxClassIDFromClassName
integer n, i
real*8 :: dat(24) = (/(i, i=1, 24, 1)/)
real*8, dimension(2,3,4) :: dats
mwSize M
parameter(M = 3)
! mwSize, dimension(M) :: dimens
integer, dimension(M) :: idimens = (/ 2, 3, 4 /)
! INTEGER, DIMENSION(5) :: ARR = (/ 1, 3, 6, 7, 9 /)
!
! Open MAT-file for writing
!
write(6,*) 'Creating MAT-file matdemo.mat ...'
mp = matOpen('matdemo.mat', 'w')
if (mp .eq. 0) then
write(6,*) 'Can''t open ''matdemo.mat'' for writing.'
write(6,*) '(Do you have write permission in this directory?)'
stop
end if
!
! Create variables
!
! dimens(1) = idimens(1)
! dimens(2) = idimens(2)
! dimens(3) = idimens(3)
! n = dimens(1)*dimens(2)*dimens(3)
! dats = reshape(dat,idimens)
! classid = mxClassIDFromClassName('double')
! pa = mxCreateNumericArray(M,dimens,classid,0)
pause
#if MX_HAS_INTERLEAVED_COMPLEX
call mxCopyReal8ToPtr(dats, mxGetDoubles(pa), n)
#else
call mxCopyReal8ToPtr(dat, mxGetPr(pa), n)
#endif
!
status = matPutVariableAsGlobal(mp, 'NumericGlobal', pa)
if (status .ne. 0) then
write(6,*) 'matPutVariableAsGlobal ''Numeric Global'' failed'
stop
end if
status = matClose(mp)
if (status .ne. 0) then
write(6,*) 'Error closing MAT-file'
stop
end if
!
! clean up memory
call mxDestroyArray(pa)
!
write(6,*) 'Done creating MAT-file'
stop
end
  댓글 수: 1
James Tursa
James Tursa 2022년 3월 8일
편집: James Tursa 2022년 3월 8일
You are probably going to need to #include "fintrf.h" in your code so that the MATLAB symbols are defined. Also, how are you compiling your code? I.e., what are the commands you are using to compile your code to ensure you are linking with the MATLAB libraries?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by