hesnengr
Petroleum
- Aug 26, 2010
- 9
Hi all,
I am writing a FORTRAN code for a MEX (Matlab executable) file. I am facing a problem in defining the dimensions of the matrix. Following is the code which I have created.
C gateway subroutine
subroutine mexfunction(nlhs, plhs, nrhs, prhs)
C-----------------------------------------------------------
integer plhs(*), prhs(*)
integer pr_in
integer mxGetPr
C----------------------------------------------------------------
integer nlhs, nrhs, mxGetM, mxGetN
integer m_in, n_in, size
m_in = mxGetM(prhs(1))
n_in = mxGetN(prhs(1))
size = m_in * n_in
pr_in = mxGetPr(prhs(1))
C call the computational routine
call compute(%val(pr_in), size,m_in,n_in)
return
end
C============================================================
C computational subroutine
subroutine compute(in_mat, size,m,n)
integer size, i,j,m,n
real*8 in_mat(*)
open(100,file='matrix.txt', status='unknown')
do i=1,size
write(100,*) in_mat(i)
enddo
close(100)
return
end
In the gateway routine I read an input matrix as pr_in(I know that it will be a 2D array) and I want to write it in a file.
This matrix is passed to the computations array as a vector which means only one dim.
I want to change this 1D array in the computational routine to a 2D i.e. from in_mat(*) to in_mat(*,*) so that it can be called as in_mat(i,j) instead of in_mat(i) while writing in the file.
write(100,*) in_mat(i,j)
Would be great if any one could give me a hint.
Thanks.
I am writing a FORTRAN code for a MEX (Matlab executable) file. I am facing a problem in defining the dimensions of the matrix. Following is the code which I have created.
C gateway subroutine
subroutine mexfunction(nlhs, plhs, nrhs, prhs)
C-----------------------------------------------------------
integer plhs(*), prhs(*)
integer pr_in
integer mxGetPr
C----------------------------------------------------------------
integer nlhs, nrhs, mxGetM, mxGetN
integer m_in, n_in, size
m_in = mxGetM(prhs(1))
n_in = mxGetN(prhs(1))
size = m_in * n_in
pr_in = mxGetPr(prhs(1))
C call the computational routine
call compute(%val(pr_in), size,m_in,n_in)
return
end
C============================================================
C computational subroutine
subroutine compute(in_mat, size,m,n)
integer size, i,j,m,n
real*8 in_mat(*)
open(100,file='matrix.txt', status='unknown')
do i=1,size
write(100,*) in_mat(i)
enddo
close(100)
return
end
In the gateway routine I read an input matrix as pr_in(I know that it will be a 2D array) and I want to write it in a file.
This matrix is passed to the computations array as a vector which means only one dim.
I want to change this 1D array in the computational routine to a 2D i.e. from in_mat(*) to in_mat(*,*) so that it can be called as in_mat(i,j) instead of in_mat(i) while writing in the file.
write(100,*) in_mat(i,j)
Would be great if any one could give me a hint.
Thanks.