Thread Subject: Fortran Mex file: copied integers are zero?

Subject: Fortran Mex file: copied integers are zero?

From: jochen

Date: 5 Nov, 2009 22:17:04

Message: 1 of 5

Hello,

this is the first time, i try to write a more complex mex-file. It compiles, but it seems that when I take values from the input parameters, they remain zero within the scope of the mex-file itself. However, I need those values to check input dimensions and for memory allocation. I tried something along those lines (whole code is long, and I post it on request too):

#include <fintrf.h>

subroutine mexfunction(nargout, pargout, nargin, pargin)

implicit none

! overhead
mwPointer :: pargout(*), pargin(*)
integer :: nargout, nargin

       mwPointer :: mxCreateDoubleMatrix, mxGetPr
mwPointer :: mxCreateNumericArray
mwSize :: mxGetM, mxGetN
integer :: mxClassIDFromClassName
! end overhead


! declarations
mwPointer :: eposp, elecnop
mwPointer :: potendp
mwSize :: m, n
character*120 :: line
integer*4 :: elecno
integer*4, allocatable, dimension(:) :: epos
real*8, allocatable, dimension(:) :: potend

! elecno
m = mxGetM(pargin(5))
n = mxGetN(pargin(5))
if ((m .ne. 1) .or. (n .ne. 1)) then
call mexErrMsgTxt('arg5: elecno !scalar!')
endif
elecnop = mxGetPr(pargin(5))
call mxCopyPtrToInteger4(elecnop,elecno,m*n)

! epos
m = mxGetM(pargin(1))
n = mxGetN(pargin(1))
if (min(m,n) .ne. 1) then
call mexErrMsgTxt('!arg1: epos nx1!')
endif
if (max(m,n) .ne. elecno) then
write (line,*) 'elecno =', elecno
call mexErrMsgTxt(line)
endif
allocate(epos(max(m,n)))
eposp = mxGetPr(pargin(1))
call mxCopyPtrToInteger4(eposp,epos,m*n)

               ....

allocate (potend(elecno))

                ...

end subroutine mexfunction


Ok, the point is the variable 'elecno' prints 0 and whatever I pass, and
  if (max(m,n) .ne. elecno) under !epos
always fails. The dimensions of elecno are checked correctly before though. Is this normal behaviour? Any help is appreciated.

Jochen

Subject: Fortran Mex file: copied integers are zero?

From: jochen

Date: 5 Nov, 2009 23:34:03

Message: 2 of 5

Ok. never mind, I found my error.

For those who are interested: the function has to be called in matlab with the arguments explicitly cast to the appropriate type (if they are not double anyway?). The call looks like that:

myfunction(...,int16(x),...)

Strangely, this does not seem to be necessary for use of variables within the function called by the mex-gateway.

jochen

Subject: Fortran Mex file: copied integers are zero?

From: jochen

Date: 5 Nov, 2009 23:39:02

Message: 3 of 5

Ok, it is solved. The solution is to cast the type when calling the function in matlab (calling myfunction(...,int16(...),...) ). This seems to play no role for inside the function called by the mex file.

(I hope this results now in a reply to my message, not in another repost - sorry to those I annoyed with that)

Subject: Fortran Mex file: copied integers are zero?

From: Luis

Date: 4 Dec, 2009 21:08:04

Message: 4 of 5

"jochen " <anzuend@gmx.at> wrote in message <hcvndb$otv$1@fred.mathworks.com>...
> Ok. never mind, I found my error.
>
> For those who are interested: the function has to be called in matlab with the arguments explicitly cast to the appropriate type (if they are not double anyway?). The call looks like that:
>
> myfunction(...,int16(x),...)
>
> Strangely, this does not seem to be necessary for use of variables within the function called by the mex-gateway.
>
> jochen

Subject: Fortran Mex file: copied integers are zero?

From: James Tursa

Date: 4 Dec, 2009 21:53:21

Message: 5 of 5

"jochen " <anzuend@gmx.at> wrote in message <hcvnmm$cnl$1@fred.mathworks.com>...
> Ok, it is solved. The solution is to cast the type when calling the function in matlab (calling myfunction(...,int16(...),...) ). This seems to play no role for inside the function called by the mex file.
>
> (I hope this results now in a reply to my message, not in another repost - sorry to those I annoyed with that)

You might also be interested in using my Fortran 95 interface code. It can be found here:

http://www.mathworks.com/matlabcentral/fileexchange/25934-fortran-95-interface-to-matlab-api-with-extras

James Tursa

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
typecasting jochen 5 Nov, 2009 18:39:04
fortran jochen 5 Nov, 2009 17:19:05
mex jochen 5 Nov, 2009 17:19:05
rssFeed for this Thread

Contact us at files@mathworks.com