From: George on
Hi,

I am learning how to use the mex functions. I have created a simple test example, which calls a m file which just times the input integer by 2. The codes are as follow:

#include "fintrf.h"
program testMain
integer input
input=1
CALL FUNC(input)
end

SUBROUTINE FUNC(a)
integer*4 plhs, prhs
integer*4 nrhs
integer*4 nlhs
character*8 name
integer a
RRHS=a
nlhs=1
nrhs=1
name="CallingTest"
call mexCallMATLAB(nlhs, plhs, nrhs, RRHS, name)
RETURN
END

Matlab m file
function times2=CallingTest(a)
times2=a*2;
end

There is no problem in Link and build. When I run it, in the line call mexCallMATLAB(nlhs, plhs, nrhs, RRHS, name)

Error appears with
Unhandled Exception : 0xC0000005: Access Violation.

It is a really simple case, but I can't figure out what is wrong. I think I am doing things following the Matlab instructions. Could some one help me?

Thanks in advance.
From: James Tursa on
"George " <guanjihou(a)gmail.com> wrote in message <humg1t$dg1$1(a)fred.mathworks.com>...
> Hi,
>
> I am learning how to use the mex functions. I have created a simple test example, which calls a m file which just times the input integer by 2. The codes are as follow:
>
> #include "fintrf.h"
> program testMain
> integer input
> input=1
> CALL FUNC(input)
> end
>
> SUBROUTINE FUNC(a)
> integer*4 plhs, prhs
> integer*4 nrhs
> integer*4 nlhs
> character*8 name
> integer a
> RRHS=a
> nlhs=1
> nrhs=1
> name="CallingTest"
> call mexCallMATLAB(nlhs, plhs, nrhs, RRHS, name)
> RETURN
> END
>
> Matlab m file
> function times2=CallingTest(a)
> times2=a*2;
> end
>
> There is no problem in Link and build. When I run it, in the line call mexCallMATLAB(nlhs, plhs, nrhs, RRHS, name)
>
> Error appears with
> Unhandled Exception : 0xC0000005: Access Violation.
>
> It is a really simple case, but I can't figure out what is wrong. I think I am doing things following the Matlab instructions. Could some one help me?
>
> Thanks in advance.

There are multiple problems.

1) You can't call mexCallMATLAB from a Fortran program. You can only call it from a mex routine that has the mexFunction interface.

2) The 2nd and 4th arguments to mexCallMATLAB need to be arrays of mxArray pointers, not regular Fortran variables. You need to build these mxArray variable pointers with the "...Create..." routines, such as mxCreateDoubleScalar, mxCreateNumericArray, etc. etc.

For example, here is a simple mex routine to call your m-file. For simplicity I simply create a scalar mxArray with the value 1.0 to use as input to your m-file. (Caution: this is untested)

#include "fintrf.h"
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
!\
! Arguments
!/
integer*4 nlhs, nrhs
mwPointer plhs(*), prhs(*)
!\
! Functions
!/
integer*4, extern :: mexCallMATLAB
integer*4, extern :: mexPrintf
mwPointer, extern :: mxCreateDoubleScalar
!\
! Local Variables
!/
integer*4 mynlhs, mynrhs
mwPointer myplhs(1), myprhs(1)
integer*4 k
!-----
myprhs(1) = mxCreateDoubleScalar(1.d0)

mynlhs = 0
mynrhs = 1
k = mexPrintf("Display the input"//achar(10))
k = mexCallMATLAB(mynlhs,myplhs,mynrhs,myprhs,"disp")

k = mexPrintf("CallingTest"//achar(10))
mynlhs = 1
mynrhs = 1
k = mexCallMATLAB(mynlhs,myplhs,mynrhs,myprhs,"CallingTest")

k = mexPrintf("Cleanup input"//achar(10))
call mxDestroyArray(myprhs(1))

mynlhs = 0
mynrhs = 1
k = mexPrintf("Display the output"//achar(10))
k = mexCallMATLAB(mynlhs,myplhs,mynrhs,myplhs,"disp")

k = mexPrintf("Cleanup output"//achar(10))
call mxDestroyArray(myplhs(1))

return
end


James Tursa
From: George on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <humj3d$r0s$1(a)fred.mathworks.com>...
> "George " <guanjihou(a)gmail.com> wrote in message <humg1t$dg1$1(a)fred.mathworks.com>...
> > Hi,
> >
> > I am learning how to use the mex functions. I have created a simple test example, which calls a m file which just times the input integer by 2. The codes are as follow:
> >
> > #include "fintrf.h"
> > program testMain
> > integer input
> > input=1
> > CALL FUNC(input)
> > end
> >
> > SUBROUTINE FUNC(a)
> > integer*4 plhs, prhs
> > integer*4 nrhs
> > integer*4 nlhs
> > character*8 name
> > integer a
> > RRHS=a
> > nlhs=1
> > nrhs=1
> > name="CallingTest"
> > call mexCallMATLAB(nlhs, plhs, nrhs, RRHS, name)
> > RETURN
> > END
> >
> > Matlab m file
> > function times2=CallingTest(a)
> > times2=a*2;
> > end
> >
> > There is no problem in Link and build. When I run it, in the line call mexCallMATLAB(nlhs, plhs, nrhs, RRHS, name)
> >
> > Error appears with
> > Unhandled Exception : 0xC0000005: Access Violation.
> >
> > It is a really simple case, but I can't figure out what is wrong. I think I am doing things following the Matlab instructions. Could some one help me?
> >
> > Thanks in advance.
>
> There are multiple problems.
>
> 1) You can't call mexCallMATLAB from a Fortran program. You can only call it from a mex routine that has the mexFunction interface.
>
> 2) The 2nd and 4th arguments to mexCallMATLAB need to be arrays of mxArray pointers, not regular Fortran variables. You need to build these mxArray variable pointers with the "...Create..." routines, such as mxCreateDoubleScalar, mxCreateNumericArray, etc. etc.
>
> For example, here is a simple mex routine to call your m-file. For simplicity I simply create a scalar mxArray with the value 1.0 to use as input to your m-file. (Caution: this is untested)
>
> #include "fintrf.h"
> subroutine mexFunction(nlhs, plhs, nrhs, prhs)
> !\
> ! Arguments
> !/
> integer*4 nlhs, nrhs
> mwPointer plhs(*), prhs(*)
> !\
> ! Functions
> !/
> integer*4, extern :: mexCallMATLAB
> integer*4, extern :: mexPrintf
> mwPointer, extern :: mxCreateDoubleScalar
> !\
> ! Local Variables
> !/
> integer*4 mynlhs, mynrhs
> mwPointer myplhs(1), myprhs(1)
> integer*4 k
> !-----
> myprhs(1) = mxCreateDoubleScalar(1.d0)
>
> mynlhs = 0
> mynrhs = 1
> k = mexPrintf("Display the input"//achar(10))
> k = mexCallMATLAB(mynlhs,myplhs,mynrhs,myprhs,"disp")
>
> k = mexPrintf("CallingTest"//achar(10))
> mynlhs = 1
> mynrhs = 1
> k = mexCallMATLAB(mynlhs,myplhs,mynrhs,myprhs,"CallingTest")
>
> k = mexPrintf("Cleanup input"//achar(10))
> call mxDestroyArray(myprhs(1))
>
> mynlhs = 0
> mynrhs = 1
> k = mexPrintf("Display the output"//achar(10))
> k = mexCallMATLAB(mynlhs,myplhs,mynrhs,myplhs,"disp")
>
> k = mexPrintf("Cleanup output"//achar(10))
> call mxDestroyArray(myplhs(1))
>
> return
> end
>
>
> James Tursa

Thanks James,

However, I still have one confusing. The Matlab help stated that mexFunction() is not a function that I can call. I am using fortran, so it is just a subroutine. My question is how can I call this subroutine in my main program?

Suppose that I have the following main program:

#include "fintrf.h"
program TestmexCallMatlab
Call mexCallfunction(1,1,1,1)
end

I tried, but it seems that I can't do this. So could you please tell me how to do this?

Thanks very much!
From: George on
"George " <guanjihou(a)gmail.com> wrote in message <humk70$6jm$1(a)fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <humj3d$r0s$1(a)fred.mathworks.com>...
> > "George " <guanjihou(a)gmail.com> wrote in message <humg1t$dg1$1(a)fred.mathworks.com>...
> > > Hi,
> > >
> > > I am learning how to use the mex functions. I have created a simple test example, which calls a m file which just times the input integer by 2. The codes are as follow:
> > >
> > > #include "fintrf.h"
> > > program testMain
> > > integer input
> > > input=1
> > > CALL FUNC(input)
> > > end
> > >
> > > SUBROUTINE FUNC(a)
> > > integer*4 plhs, prhs
> > > integer*4 nrhs
> > > integer*4 nlhs
> > > character*8 name
> > > integer a
> > > RRHS=a
> > > nlhs=1
> > > nrhs=1
> > > name="CallingTest"
> > > call mexCallMATLAB(nlhs, plhs, nrhs, RRHS, name)
> > > RETURN
> > > END
> > >
> > > Matlab m file
> > > function times2=CallingTest(a)
> > > times2=a*2;
> > > end
> > >
> > > There is no problem in Link and build. When I run it, in the line call mexCallMATLAB(nlhs, plhs, nrhs, RRHS, name)
> > >
> > > Error appears with
> > > Unhandled Exception : 0xC0000005: Access Violation.
> > >
> > > It is a really simple case, but I can't figure out what is wrong. I think I am doing things following the Matlab instructions. Could some one help me?
> > >
> > > Thanks in advance.
> >
> > There are multiple problems.
> >
> > 1) You can't call mexCallMATLAB from a Fortran program. You can only call it from a mex routine that has the mexFunction interface.
> >
> > 2) The 2nd and 4th arguments to mexCallMATLAB need to be arrays of mxArray pointers, not regular Fortran variables. You need to build these mxArray variable pointers with the "...Create..." routines, such as mxCreateDoubleScalar, mxCreateNumericArray, etc. etc.

Regarding to why I am using

integer*4 plhs(*), prhs(*)

instead of mwPointer plhs(*), prhs(*),

When I use the latter one, there are some error in the like, as follow:

rror: Syntax error, found IDENTIFIER 'PLHS' when expecting one of: ( : % . = =>
mwPointer plhs(*), prhs(*)

I do include the header file, I just can't figure out what is wrong. But when I use the former declaration, this error message would not appear. Do you know why?

Thanks very much.


> >
> > For example, here is a simple mex routine to call your m-file. For simplicity I simply create a scalar mxArray with the value 1.0 to use as input to your m-file. (Caution: this is untested)
> >
> > #include "fintrf.h"
> > subroutine mexFunction(nlhs, plhs, nrhs, prhs)
> > !\
> > ! Arguments
> > !/
> > integer*4 nlhs, nrhs
> > mwPointer plhs(*), prhs(*)
> > !\
> > ! Functions
> > !/
> > integer*4, extern :: mexCallMATLAB
> > integer*4, extern :: mexPrintf
> > mwPointer, extern :: mxCreateDoubleScalar
> > !\
> > ! Local Variables
> > !/
> > integer*4 mynlhs, mynrhs
> > mwPointer myplhs(1), myprhs(1)
> > integer*4 k
> > !-----
> > myprhs(1) = mxCreateDoubleScalar(1.d0)
> >
> > mynlhs = 0
> > mynrhs = 1
> > k = mexPrintf("Display the input"//achar(10))
> > k = mexCallMATLAB(mynlhs,myplhs,mynrhs,myprhs,"disp")
> >
> > k = mexPrintf("CallingTest"//achar(10))
> > mynlhs = 1
> > mynrhs = 1
> > k = mexCallMATLAB(mynlhs,myplhs,mynrhs,myprhs,"CallingTest")
> >
> > k = mexPrintf("Cleanup input"//achar(10))
> > call mxDestroyArray(myprhs(1))
> >
> > mynlhs = 0
> > mynrhs = 1
> > k = mexPrintf("Display the output"//achar(10))
> > k = mexCallMATLAB(mynlhs,myplhs,mynrhs,myplhs,"disp")
> >
> > k = mexPrintf("Cleanup output"//achar(10))
> > call mxDestroyArray(myplhs(1))
> >
> > return
> > end
> >
> >
> > James Tursa
>
> Thanks James,
>
> However, I still have one confusing. The Matlab help stated that mexFunction() is not a function that I can call. I am using fortran, so it is just a subroutine. My question is how can I call this subroutine in my main program?
>
> Suppose that I have the following main program:
>
> #include "fintrf.h"
> program TestmexCallMatlab
> Call mexCallfunction(1,1,1,1)
> end
>
> I tried, but it seems that I can't do this. So could you please tell me how to do this?
>
> Thanks very much!
From: James Tursa on
"George " <guanjihou(a)gmail.com> wrote in message <humk70$6jm$1(a)fred.mathworks.com>...
>
> However, I still have one confusing. The Matlab help stated that mexFunction() is not a function that I can call. I am using fortran, so it is just a subroutine. My question is how can I call this subroutine in my main program?
>
> Suppose that I have the following main program:
>
> #include "fintrf.h"
> program TestmexCallMatlab
> Call mexCallfunction(1,1,1,1)
> end
>
> I tried, but it seems that I can't do this. So could you please tell me how to do this?

You can't. mexFunction is a gateway routine that MATLAB calls when you invoke the function from MATLAB. It is not something you call yourself.

James Tursa