|
On Wed, 24 Oct 2007 22:48:41 +0000 (UTC), "Jerry "
<mricad@yahoo.no000spppam.com> wrote:
>please open the simple MATLAB example sincall.F (and fill.F)
>edit([matlabroot '/extern/examples/refbook/sincall.F'])
>
>My question is: how to modify this code such that one can
>specify the parameter max and return the lhs in the code via
>result=sincall(max)? I always get a run-time segmentation error.
>
>This may help me solve the problem in my previous post which
>is too long.
>
>Thanks very much for your kind help!
I don't have a Fortran compiler handy to check this out, but here is
something to try. The two critical changes are
1) The mxCopyPtrToReal8 call (be sure to use the mxGetPr call here
also since mxCopyPtrToReal8 needs the pointer to the data in an
mxArray, not the pointer to an mxArray itself) to get rmax and then
use this to get max (btw, I have to say that this is a lousy variable
name to use since it matches a Fortran intrinsic function name). I was
too lazy to put in all of the class checking, etc., that one would
normally put here. If you need help with that also then let me know.
2) Don't destroy lhs(1) at the end. Simply set plhs(1) equal to it.
You don't have to check that nlhs > 0 here because you can always set
plhs(1) even when nlhs == 0 ... the result simply goes into ans in
that case.
Also keep in mind that later versions of MATLAB use mwsize types for
the arguments to mxCreateDoubleMatrix, not integer specifically.
James Tursa
#include "fintrf.h"
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
integer nlhs, nrhs
mwpointer plhs(*), prhs(*)
!-----------------------------------------------------------------------
mwpointer rhs(1), lhs(1)
mwpointer mxGetPr, mxCreateDoubleMatrix
integer m, n, max
real*8 rmax
integer*4 one
one = 1
! initializition
m=1
n=1
! max=1000
!\
! Note: Didn't check for correct input here. If you don't pass in a
double
! as the first argument then this routine will likely bomb with a
"stack dump"
!/
call mxCopyPtrToReal8(mxGetPr(prhs(1)), rmax, one)
max = rmax
rhs(1) = mxCreateDoubleMatrix(max, 1, 0)
! pass the pointer and variable and let fill() fill up data
call fill(%VAL(mxGetPr(rhs(1))), m, n, max)
call mxSetM(rhs(1), m)
call mxSetN(rhs(1), n)
call mexCallMATLAB(1, lhs, 1, rhs, 'sin')
call mexCallMATLAB(0, NULL, 1, lhs, 'plot')
! cleanup the un-freed memory after calling mexCallMATLAB.
call mxDestroyArray(rhs(1))
! call mxDestroyArray(lhs(1))
plhs(1) = lhs(1)
return
end
|