主题:matlab调用fortran中,变量传递问题,请帮忙
最近在搞matlab调用fortran,其中变量传递出现了问题,调用程序中用到了module,其中的变量大小是由传递的变量定义的,程序如下,mex就出错,请帮忙看一下,谢谢!
#include "fintrf.h"
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
mwpointer plhs(*), prhs(*)
mwpointer mxCreateDoubleMatrix
mwpointer mxGetPr
mwpointer x_pr, y_pr, z_pr
integer nlhs, nrhs, mxIsNumeric
mwsize m, n, size, row
mwsize mxGetM, mxGetN
real*8 x, y(100), z(100)
if (nrhs .ne. 2) then
call mexErrMsgTxt('Two inputs required.')
elseif (nlhs .ne. 1) then
call mexErrMsgTxt('One output required.')
endif
if (mxIsNumeric(prhs(1)) .ne. 1) then
call mexErrMsgTxt('Input # 1 is not a numeric.')
elseif (mxIsNumeric(prhs(2)) .ne. 1) then
call mexErrMsgTxt('Input #2 is not a numeric array.')
endif
m = mxGetM(prhs(1))
n = mxGetN(prhs(1))
if(n .ne. 1 .or. m .ne. 1) then
call mexErrMsgTxt('Input #1 is not a scalar.')
endif
m = mxGetM(prhs(2))
n = mxGetN(prhs(2))
size = m*n
if(size .gt. 100) then
call mexErrMsgTxt('Input #2 number of elements exceeds buffer')
endif
plhs(1) = mxCreateDoubleMatrix(m,n,0)
x_pr = mxGetPr(prhs(1))
y_pr = mxGetPr(prhs(2))
z_pr = mxGetPr(plhs(1))
row = 1
call mxCopyPtrToReal8(x_pr,x,row)
call mxCopyPtrToReal8(y_pr,y,size)
call xtimesy(x,y,z,m,n)
call mxCopyReal8ToPtr(z,z_pr,size) !ddddadfa
!!dddd
return
end
module zzz
integer :: mm
integer aa(m)
end module
subroutine xtimesy(x,y,z,m,n)
use zzz
real*8 x, y(m,n), z(m,n),zz(m,n)
integer i, j
do i=1,m
do j=1,n
z(i,j)=x*y(i,j)
end do
end do
return
end
#include "fintrf.h"
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
mwpointer plhs(*), prhs(*)
mwpointer mxCreateDoubleMatrix
mwpointer mxGetPr
mwpointer x_pr, y_pr, z_pr
integer nlhs, nrhs, mxIsNumeric
mwsize m, n, size, row
mwsize mxGetM, mxGetN
real*8 x, y(100), z(100)
if (nrhs .ne. 2) then
call mexErrMsgTxt('Two inputs required.')
elseif (nlhs .ne. 1) then
call mexErrMsgTxt('One output required.')
endif
if (mxIsNumeric(prhs(1)) .ne. 1) then
call mexErrMsgTxt('Input # 1 is not a numeric.')
elseif (mxIsNumeric(prhs(2)) .ne. 1) then
call mexErrMsgTxt('Input #2 is not a numeric array.')
endif
m = mxGetM(prhs(1))
n = mxGetN(prhs(1))
if(n .ne. 1 .or. m .ne. 1) then
call mexErrMsgTxt('Input #1 is not a scalar.')
endif
m = mxGetM(prhs(2))
n = mxGetN(prhs(2))
size = m*n
if(size .gt. 100) then
call mexErrMsgTxt('Input #2 number of elements exceeds buffer')
endif
plhs(1) = mxCreateDoubleMatrix(m,n,0)
x_pr = mxGetPr(prhs(1))
y_pr = mxGetPr(prhs(2))
z_pr = mxGetPr(plhs(1))
row = 1
call mxCopyPtrToReal8(x_pr,x,row)
call mxCopyPtrToReal8(y_pr,y,size)
call xtimesy(x,y,z,m,n)
call mxCopyReal8ToPtr(z,z_pr,size) !ddddadfa
!!dddd
return
end
module zzz
integer :: mm
integer aa(m)
end module
subroutine xtimesy(x,y,z,m,n)
use zzz
real*8 x, y(m,n), z(m,n),zz(m,n)
integer i, j
do i=1,m
do j=1,n
z(i,j)=x*y(i,j)
end do
end do
return
end