<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158280</link>
    <title>MATLAB Central Newsreader - MEX FORTRAN -- please help!</title>
    <description>Feed for thread: MEX FORTRAN -- please help!</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 24 Oct 2007 22:48:41 -0400</pubDate>
      <title>MEX FORTRAN -- please help!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158280#398274</link>
      <author>Jerry </author>
      <description>please open the simple MATLAB example sincall.F (and fill.F)&lt;br&gt;
edit([matlabroot '/extern/examples/refbook/sincall.F'])&lt;br&gt;
&lt;br&gt;
My question is: how to modify this code such that one can&lt;br&gt;
specify the parameter max and return the lhs in the code via&lt;br&gt;
result=sincall(max)? I always get a run-time segmentation error.&lt;br&gt;
&lt;br&gt;
This may help me solve the problem in my previous post which&lt;br&gt;
is too long.&lt;br&gt;
&lt;br&gt;
Thanks very much for your kind help!</description>
    </item>
    <item>
      <pubDate>Thu, 25 Oct 2007 08:06:50 -0400</pubDate>
      <title>Re: MEX FORTRAN -- please help!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158280#398316</link>
      <author>James Tursa</author>
      <description>On Wed, 24 Oct 2007 22:48:41 +0000 (UTC), &quot;Jerry &quot;&lt;br&gt;
&amp;lt;mricad@yahoo.no000spppam.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;please open the simple MATLAB example sincall.F (and fill.F)&lt;br&gt;
&amp;gt;edit([matlabroot '/extern/examples/refbook/sincall.F'])&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;My question is: how to modify this code such that one can&lt;br&gt;
&amp;gt;specify the parameter max and return the lhs in the code via&lt;br&gt;
&amp;gt;result=sincall(max)? I always get a run-time segmentation error.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;This may help me solve the problem in my previous post which&lt;br&gt;
&amp;gt;is too long.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;Thanks very much for your kind help!&lt;br&gt;
&lt;br&gt;
I don't have a Fortran compiler handy to check this out, but here is&lt;br&gt;
something to try. The two critical changes are&lt;br&gt;
&lt;br&gt;
1) The mxCopyPtrToReal8 call (be sure to use the mxGetPr call here&lt;br&gt;
also since mxCopyPtrToReal8 needs the pointer to the data in an&lt;br&gt;
mxArray, not the pointer to an mxArray itself) to get rmax and then&lt;br&gt;
use this to get max (btw, I have to say that this is a lousy variable&lt;br&gt;
name to use since it matches a Fortran intrinsic function name). I was&lt;br&gt;
too lazy to put in all of the class checking, etc., that one would&lt;br&gt;
normally put here. If you need help with that also then let me know.&lt;br&gt;
&lt;br&gt;
2) Don't destroy lhs(1) at the end. Simply set plhs(1) equal to it.&lt;br&gt;
You don't have to check that nlhs &amp;gt; 0 here because you can always set&lt;br&gt;
plhs(1) even when nlhs == 0 ... the result simply goes into ans in&lt;br&gt;
that case.&lt;br&gt;
&lt;br&gt;
Also keep in mind that later versions of MATLAB use mwsize types for&lt;br&gt;
the arguments to mxCreateDoubleMatrix, not integer specifically.&lt;br&gt;
&lt;br&gt;
James Tursa&lt;br&gt;
&lt;br&gt;
#include &quot;fintrf.h&quot;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;subroutine mexFunction(nlhs, plhs, nrhs, prhs)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;integer nlhs, nrhs&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mwpointer plhs(*), prhs(*)&lt;br&gt;
!-----------------------------------------------------------------------&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mwpointer rhs(1), lhs(1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mwpointer mxGetPr, mxCreateDoubleMatrix&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;integer m, n, max&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;real*8 rmax&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;integer*4 one&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;one = 1&lt;br&gt;
&lt;br&gt;
!     initializition&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m=1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n=1&lt;br&gt;
!     max=1000&lt;br&gt;
!\&lt;br&gt;
! Note: Didn't check for correct input here. If you don't pass in a&lt;br&gt;
double&lt;br&gt;
! as the first argument then this routine will likely bomb with a&lt;br&gt;
&quot;stack dump&quot;&lt;br&gt;
!/&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call mxCopyPtrToReal8(mxGetPr(prhs(1)), rmax, one)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;max = rmax&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rhs(1) = mxCreateDoubleMatrix(max, 1, 0)&lt;br&gt;
&amp;nbsp;&lt;br&gt;
!     pass the pointer and variable and let fill() fill up data&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call fill(%VAL(mxGetPr(rhs(1))), m, n, max)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call mxSetM(rhs(1), m)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call mxSetN(rhs(1), n)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call mexCallMATLAB(1, lhs, 1, rhs, 'sin')&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call mexCallMATLAB(0, NULL, 1, lhs, 'plot')&lt;br&gt;
&lt;br&gt;
!     cleanup the un-freed memory after calling mexCallMATLAB.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;call mxDestroyArray(rhs(1))&lt;br&gt;
!     call mxDestroyArray(lhs(1))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;plhs(1) = lhs(1)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end  </description>
    </item>
    <item>
      <pubDate>Thu, 25 Oct 2007 08:22:58 -0400</pubDate>
      <title>Re: MEX FORTRAN -- please help!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158280#398318</link>
      <author>James Tursa</author>
      <description>&lt;br&gt;
P.S.  The %VAL stuff is old school F77 style code. If you have a F95&lt;br&gt;
compiler you can write much cleaner code by using allocatable Fortran&lt;br&gt;
variables to set the size dynamically at run time depending on the&lt;br&gt;
value of the input max. You may pay a penalty in this case, however,&lt;br&gt;
by having to copy the data into the mxArray after it is generated, an&lt;br&gt;
extra step that you may not have needed by using the %VAL construct.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Thu, 25 Oct 2007 15:18:12 -0400</pubDate>
      <title>Re: MEX FORTRAN -- please help!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/158280#398387</link>
      <author>Jerry </author>
      <description>James Tursa &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;i3k0i3hbk93qlos90pl832289oq1t0nd1u@4ax.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; P.S.  The %VAL stuff is old school F77 style code. If you&lt;br&gt;
have a F95&lt;br&gt;
&amp;gt; compiler you can write much cleaner code by using&lt;br&gt;
allocatable Fortran&lt;br&gt;
&amp;gt; variables to set the size dynamically at run time&lt;br&gt;
depending on the&lt;br&gt;
&amp;gt; value of the input max. You may pay a penalty in this&lt;br&gt;
case, however,&lt;br&gt;
&amp;gt; by having to copy the data into the mxArray after it is&lt;br&gt;
generated, an&lt;br&gt;
&amp;gt; extra step that you may not have needed by using the %VAL&lt;br&gt;
construct.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; James Tursa&lt;br&gt;
&amp;gt; &lt;br&gt;
Thanks very much, Dr. Tursa, and for your reply to my other&lt;br&gt;
post. My code is still too messy to post here so I tried to&lt;br&gt;
figure out the problem with this simple example instead.&lt;br&gt;
Your code works very well! I must be very careful with the&lt;br&gt;
communication between Matlab and Fortran. I'll be serious on&lt;br&gt;
my own code now -- let's see! -- I promise to share all my&lt;br&gt;
experience with everyone here if it finally works well.&lt;br&gt;
&lt;br&gt;
what's the email of Matlab support? I tried to run my Matlab&lt;br&gt;
under gdb but got a license problem. When I run&lt;br&gt;
linux&amp;gt; matlab -Dgdb&lt;br&gt;
I had NO license problem; then when I run&lt;br&gt;
&amp;lt;gdb&amp;gt; run -nojvm&lt;br&gt;
I got a license error!&lt;br&gt;
&lt;br&gt;
Thanks again for your help.</description>
    </item>
  </channel>
</rss>

