Writing mat file from fortran
3 views (last 30 days)
Show older comments
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 Comment
James Tursa
on 8 Mar 2022
Edited: James Tursa
on 8 Mar 2022
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?
Answers (0)
See Also
Categories
Find more on Write Fortran Programs to Read MAT-File Data 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!