Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Call a MEX function from Matlab Engine?
Date: Sat, 15 Mar 2008 07:23:03 +0000 (UTC)
Organization: Boeing
Lines: 79
Message-ID: <frftgn$630$1@fred.mathworks.com>
References: <frc4bp$ipi$1@fred.mathworks.com> <frc7ir$s19$1@fred.mathworks.com> <frccfq$l2f$1@fred.mathworks.com> <frch5p$bdj$1@fred.mathworks.com> <frdvs6$lim$1@fred.mathworks.com> <fre31g$bk1$1@fred.mathworks.com> <fre6af$jpp$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1205565783 6240 172.30.248.38 (15 Mar 2008 07:23:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 15 Mar 2008 07:23:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:457365




Here is a complete working example. Maybe by comparing this
to what you have you can decipher what is wrong with your
code.  Call the following file myfunc.cpp and make sure it
is on the MATLAB path. This is a simple mex function that
computes a quadratic function with obvious min at x = 4.0.
-------------------------------------------------
#include "mex.h"
#include "matrix.h"

double quadratic(double x)
{
  return 3.0 + (x - 4.0)*(x - 4.0);
}

void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
  plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);
  *mxGetPr(plhs[0]) = quadratic(*mxGetPr(prhs[0]));
}
-------------------------------------------------
mex it as follows:

>> mex myfunc.cpp

Then call the following file engfunc.cpp. I used fminsearch
instead of fminunc for this example. This program opens the
engine, creates a function handle to the mex function
myfunc, calls fminsearch to find the minimum of this mex
function starting with a guess of 0, then gets the result
into the C++ code and displays it. I left the engine
running, but of course you could close it if you wanted to.
-------------------------------------------------
#include <iostream>
#include "engine.h"

using namespace std;

int main()
{
    Engine *ep;
    mxArray *test2;
    
    ep = engOpen( NULL );
    engEvalString( ep, "fun = @myfunc;" );
    engEvalString( ep, "a = fminsearch(fun,0);" );
    if( test2 = engGetVariable( ep, "a" ) )
    {
        cout << *mxGetPr( test2 ) << endl;
        mxDestroyArray( test2 );
    }
    else
    {
        cout << "Didn't work" << endl;
    }
}
-------------------------------------------------
I compiled it as follows for VC++ 8.0:
>> options = [matlabroot
'\bin\win32\mexopts\msvc80engmatopts.bat'];
>> mex('-f', options, 'engfunc.cpp', '-v');

You will of course have to modify this for your particular
compiler.

Then you can execute it as follows to find the minimum of
myfunc and display it to the screen from your engine app:

>> !engfunc
    4

I haven't included any error checking in the code to keep
things simple for the example. Well, no, actually the real
reason is that it is late and I am tired and just lazy, but
pretend the first reason I just gave is the correct one.

James Tursa