Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
MEX FORTRAN -- please help!

Subject: MEX FORTRAN -- please help!

From: Jerry

Date: 24 Oct, 2007 22:48:41

Message: 1 of 4

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!

Subject: MEX FORTRAN -- please help!

From: James Tursa

Date: 25 Oct, 2007 08:06:50

Message: 2 of 4

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

Subject: MEX FORTRAN -- please help!

From: James Tursa

Date: 25 Oct, 2007 08:22:58

Message: 3 of 4


P.S. The %VAL stuff is old school F77 style code. If you have a F95
compiler you can write much cleaner code by using allocatable Fortran
variables to set the size dynamically at run time depending on the
value of the input max. You may pay a penalty in this case, however,
by having to copy the data into the mxArray after it is generated, an
extra step that you may not have needed by using the %VAL construct.

James Tursa

Subject: MEX FORTRAN -- please help!

From: Jerry

Date: 25 Oct, 2007 15:18:12

Message: 4 of 4

James Tursa <aclassyguywithaknotac@hotmail.com> wrote in
message <i3k0i3hbk93qlos90pl832289oq1t0nd1u@4ax.com>...
>
> P.S. The %VAL stuff is old school F77 style code. If you
have a F95
> compiler you can write much cleaner code by using
allocatable Fortran
> variables to set the size dynamically at run time
depending on the
> value of the input max. You may pay a penalty in this
case, however,
> by having to copy the data into the mxArray after it is
generated, an
> extra step that you may not have needed by using the %VAL
construct.
>
> James Tursa
>
Thanks very much, Dr. Tursa, and for your reply to my other
post. My code is still too messy to post here so I tried to
figure out the problem with this simple example instead.
Your code works very well! I must be very careful with the
communication between Matlab and Fortran. I'll be serious on
my own code now -- let's see! -- I promise to share all my
experience with everyone here if it finally works well.

what's the email of Matlab support? I tried to run my Matlab
under gdb but got a license problem. When I run
linux> matlab -Dgdb
I had NO license problem; then when I run
<gdb> run -nojvm
I got a license error!

Thanks again for your help.

Tags for this Thread

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.

Contact us