zfx 发表于 2022-4-12 15:39:54

有大神用过fortran调用matlab中mexcallmatlab吗?下面是程序

!!!!!!!!!!!!!!!运行错误,提示栈溢出,想先试试可不可以这样调用sin,然后再调用自定义函数
module matlab
implicit none

integer(8) ep !指针,用于指向打开的matlab
integer(8) status !非指针,记录命令执行的结果是否有效

integer(8),external::engOpen,engPutVariable,engGetVariable,engEvalString,engClose
integer(8),external::mxCreateDoubleMatrix,mxGetPr,mexCallMATLAB

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
!!========================================
!!将fortran中的矩阵送到matlab中去
subroutine f2m(fdata,mstring,row,col,ptemp)
implicit none
integer(8) row,col
real(8) fdata(1:row,1:col)
character(*)mstring
integer(8) ptemp
ptemp = mxCreateDoubleMatrix(row,col,0) !!!mxCreatDoubleMatrix新建一个double 类型数组,m代表行数,n代表列数,如果含复数开关为1,不含为0!
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 引擎工作空间写入变量。

write(*,*) status
pause
!call mxDestroyArray(ptemp) !!!释放内存

print *,"正在matlab中生成矩阵: ",mstring

endsubroutine f2m
!!==============================================
!!将matlab中的矩阵输入到Fortran中
subroutine m2f(mstring,ddata,row,col)
implicit none
integer(8) row,col
real(8) ddata(row,col)
character(*)mstring
integer(8) ptemp
ptemp = engGetVariable(ep,mstring) !!!获得当前 Matlab 窗口的显示 / 隐藏情况,可以调用函数:
call mxCopyPtrToReal8(mxGetPr(ptemp),ddata,row*col)

endsubroutine m2f

subroutine callm(nlhs, plhs, nrhs, prhs,functionname)
implicit none
integer(8)nlhs,plhs,nrhs,prhs
character(*) functionname
status = mexCallMATLAB(nlhs, plhs, nrhs, prhs,functionname)

if(status /= 0)then !!成功返回0
write(*,*) '调用失败'
stop
end if
end subroutine callm

    endmodule matlab

Program main
use matlab
      implicit none
      integer(8),parameter :: ndata=3375
      real(8) x(ndata)
      integer(8)::m,px,py,pz,n,pout



do m=1,ndata
      x(m)=m

end do
call startmatlab()
call f2m(x,"x",ndata,1,px)
write(*,*) px
call callm(1, pout, 1, px, " 'sin' ")
call closematlab()
pause
end Program main

cqflhl 发表于 2023-7-14 10:21:42

mex文件既然是C、Fortran编译的,是不是可以不用matlab环境了。为何还要打开关闭matlab呢。能不能把matlab里的mex文件和Fortran的脚本一起编译呢

辰囵1 发表于 2023-8-2 11:59:54

我的理解是:mex是matlab来调用Fortran的一个文件,如果想要在Fortran中调用matlab,则需要使用engopen引擎来开启调用通道,不知道这种理解是否正确

cqflhl 发表于 2023-9-8 18:03:59

辰囵1 发表于 2023-8-2 11:59
我的理解是:mex是matlab来调用Fortran的一个文件,如果想要在Fortran中调用matlab,则需要使用engopen引擎 ...

言外之意, 不能直接在Fortran里调用现成的matlabmex文件?
页: [1]
查看完整版本: 有大神用过fortran调用matlab中mexcallmatlab吗?下面是程序