|
Hi:
I am trying to use fortran to call matlab software by using fortran mex file and matlab engine. I came across some problems:
I wrote a main program and a subroutine in fortran. and try to use all engine functions in the subroutine
when I put the main program and the subroutine in different files, I use:
mex ('-f', [matlabroot '\bin\win32\mexopts\intelf91msvs2005engmatopts.bat'],'interpo.f','sub.f')
to compile these two files. it doesn't work. the error information is:
sub.f(7) : Error: Syntax error, found ',' when expecting one of: => = . ( : %
mwpointer engOpen, engGetVariable, mxCreateDoubleMatrix
------------------------^
sub.f(8) : Error: Syntax error, found END-OF-STATEMENT when expecting one of: => = . ( : %
mwpointer mxGetPr
------------------------^
sub.f(9) : Error: Syntax error, found ',' when expecting one of: => = . ( : %
mwpointer ep, mtdataz, mtdatapoc, mtrealz, mtrealpoc
-------------------^
sub.f(23) : Error: This name does not have a type, and must have an explicit type. [EP]
ep = engOpen('matlab ')
------^
sub.f(23) : Error: This name does not have a type, and must have an explicit type. [ENGOPEN]
ep = engOpen('matlab ')
-----------^
sub.f(34) : Error: This name does not have a type, and must have an explicit type. [MXGETPR]
call mxCopyReal8ToPtr(dataz, mxGetPr(mtdataz),7)
However, when I put the main program and the subroutine in one file and compile it, it works!!
mex ('-f', [matlabroot '\bin\win32\mexopts\intelf91msvs2005engmatopts.bat'],'interpo.f')
I just don't know why it doesn't work when I put them in separate files .Thank you so much!!
Here is my code:
program interpo
implicit none
real*8 realpoc(402)
call execute(realpoc)
write(*,*)realpoc
stop
end
subroutine execute(realpoc)
implicit none
integer pass
double precision datapoc(7),dataz(7),realz(402),realpoc(402)
integer engPutVariable, engEvalString, engClose
integer temp, i, status
mwpointer engOpen, engGetVariable, mxCreateDoubleMatrix
mwpointer mxGetPr
mwpointer ep, mtdataz, mtdatapoc, mtrealz, mtrealpoc
C initial value for z and the poc data
data dataz / 0, 350, 355, 365, 390, 395, 400 /
data datapoc / 0.00001, 2, 5, 10, 13, 17, 63 /
realz(1)=0
realz(2)=0.5
do i=3,401
realz(i)=realz(i-1)+1
enddo
realz(402)=400
C
ep = engOpen('matlab ')
if (ep .eq. 0) then
write(6,*) 'Can''t start Matlab engine'
stop
endif
mtdataz = mxCreateDoubleMatrix(1,7,0)
mtdatapoc = mxCreateDoubleMatrix(1,7,0)
mtrealz = mxCreateDoubleMatrix(1,402,0)
call mxCopyReal8ToPtr(dataz, mxGetPr(mtdataz),7)
call mxCopyReal8toPtr(datapoc,mxGetPr(mtdatapoc),7)
call mxCopyReal8toPtr(realz,mxGetPr(mtrealz),402)
status = engPutVariable(ep, 'mtdataz', mtdataz)
if (status .ne. 0) then
write(6,*) 'engPutVariable failed'
stop
endif
status = engPutVariable(ep, 'mtdatapoc', mtdatapoc)
if (status .ne. 0) then
write(6,*) 'engPutVariable failed'
stop
endif
status = engPutVariable(ep, 'mtrealz', mtrealz)
if (status .ne. 0) then
write(6,*) 'engPutVariable failed'
stop
endif
if (engEvalString(ep, 'mtrealpoc=interp1(mtdataz,mtdatapoc,mtrealz
& ,''cubic'');') .ne. 0) then
write(6, *) 'engEvalString failed'
stop
endif
if (engEvalString(ep, 'plot(mtrealpoc,mtrealz);') .ne. 0) then
write(6, *) 'engEvalString failed'
stop
endif
print *, 'Type 0 <return> to Exit'
print *, 'Type 1 <return> to continue'
read(*,*) temp
if (temp .eq. 0) then
print *,'EXIT!'
status = engClose(ep)
if (status .ne. 0) then
write(6, *) 'engClose failed'
end if
stop
endif
mtrealpoc = engGetVariable(ep, 'mtrealpoc')
call mxCopyPtrToReal8(mxGetPr(mtrealpoc), realpoc, 402)
call mxDestroyArray(mtrealz)
call mxDestroyArray(mtrealpoc)
call mxDestroyArray(mtdataz)
call mxDestroyArray(mtdatapoc)
status = engClose(ep)
if (status .ne. 0) then
write(6, *) 'engClose failed'
stop
endif
return
end subroutine
|