[Fortran] 纯文本查看 复制代码
module matlab
implicit none
integer ep !指针,用于指向打开的matlab
integer status !非指针,记录命令执行的结果是否有效
integer,external::engOpen,engPutVariable,engGetVariable,engEvalString,engClose
integer,external::mxCreateDoubleMatrix,mxGetPr
contains
!!======================================
!!打开matlab应用程序
subroutine startmatlab()
implicit none
write(*,*)"正在打开matlab应用程序,请稍后......."
ep = engOpen('matlab')
if(ep==0)then
write(*,*)"未能打开matlab应用程序,程序结束"
stop
else
write(*,*)"成功打开matlab应用程序窗口"
endif
endsubroutine startmatlab
!!===================================
!!关闭matlab应用程序
subroutine closematlab()
implicit none
write(*,*)"正在关闭matlab窗口,请稍后......"
status = engClose(ep)
if(status /=0 )then
write(*,*)"未能关闭matlab程序窗口,程序结束"
stop
else
write(*,*)"成功关闭matlab的程序窗口"
endif
endsubroutine closematlab
!!========================================
!!在matlab中执行命令
subroutine mc(string)
implicit none
character(*) string
print *,"正在matlab中计算语句: ",string
if(engEvalString(ep,string) /=0 )then
!!!engEvalString发送命令让 Matlab 执行,参数 ep 为函数 engOpen 返回的引擎指针,字符串 string 为要 matlab 执行的命令。
write(6,*) 'engEvalString failed'
stop
endif
endsubroutine mc
!!==========================================
!!将fortran中的矩阵送到matlab中去
subroutine f2m(fdata,mstring,row,col)
implicit none
integer row,col
real(8) fdata(1:row,1:col)
character(*)mstring
integer ptemp
ptemp = mxCreateDoubleMatrix(row,col,0) !!!mxCreatDoubleMatrix新建一个double 类型数组
if(ptemp==0)then
write(*,*)"无法申请内存"
stop
endif
call mxCopyReal8ToPtr(fdata,mxGetPr(ptemp),row*col)
!!!mxCopyReal8ToPtr将一个Fortran语言的实数类型数组中的数据复制到某个阵列的实数部分或虚数部分中。
!!!fdata为fortran语言的实数类型数组
!!!mxGetPr(ptemp)为指向某个阵列的实数或虚数部分的数据的指针;mxGetPr用来获取矩阵指针
!!!row*col为希望复制的元素的个数
status = engPutVariable(ep,mstring,ptemp) !!!向 Matlab 引擎工作空间写入变量。
call mxDestroyArray(ptemp) !!!释放内存
print *,"正在matlab中生成矩阵: ",mstring
if(status /= 0)then !!成功返回0
write(*,*) 'engPutVariable failed'
stop
endif
endsubroutine f2m
!!==============================================
!!将matlab中的矩阵输入到Fortran中
subroutine m2f(mstring,ddata,row,col)
integer row,col
real(8) ddata(row,col)
character(*)mstring
integer ptemp
ptemp = engGetVariable(ep,mstring) !!!获得当前 Matlab 窗口的显示 / 隐藏情况,可以调用函数:
call mxCopyPtrToReal8(mxGetPr(ptemp),ddata,row*col)
endsubroutine m2f
endmodule matlab
Program main
use matlab
implicit none
integer,parameter :: ndata=10
real(8)::x(ndata),y(ndata),z(ndata)
integer::m
do m=1,ndata
x(m)=3.1415926587*(m-1)/(ndata-1)
y(m)=sin(x(m))
end do
call startmatlab()
call f2m(x,"x",1,ndata)
call f2m(y,"y",1,ndata)
call mc("plot(x,y);title('y=sin(x)');xlabel('x');ylabel('y')")
call mc("z=2*y")
call m2f("z",z,1,ndata)
print *," x "," y "," z "," 2*y"
do m=1,ndata
write(*,"(G15.6,G15.6,G15.6,G15.6)") x(m),y(m),z(m),2*y(m)
enddo
pause
end Program main