Re: Call a MEX function from Matlab Engine?
- From: "David Doria" <daviddoria@xxxxxxxxx>
- Date: Sat, 15 Mar 2008 11:22:04 +0000 (UTC)
James, thanks for the example. However, I was able to get
that way working. What I need is for quadratic(double x) to
be in engfunc.cpp. This is my "main" program in which the
cost function relies on many other functions - I cannot
transfer them all to myfunc.cpp.
Is this possible?
"James Tursa" <aclassyguywithaknotac@xxxxxxxxxxx> wrote in
message <frftgn$630$1@xxxxxxxxxxxxxxxxxx>...
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:
'\bin\win32\mexopts\msvc80engmatopts.bat'];options = [matlabroot
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:
4!engfunc
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
.
- Follow-Ups:
- Re: Call a MEX function from Matlab Engine?
- From: James Tursa
- Re: Call a MEX function from Matlab Engine?
- References:
- Call a MEX function from Matlab Engine?
- From: David Doria
- Re: Call a MEX function from Matlab Engine?
- From: James Tursa
- Re: Call a MEX function from Matlab Engine?
- From: David Doria
- Re: Call a MEX function from Matlab Engine?
- From: James Tursa
- Re: Call a MEX function from Matlab Engine?
- From: David Doria
- Re: Call a MEX function from Matlab Engine?
- From: James Tursa
- Re: Call a MEX function from Matlab Engine?
- From: David Doria
- Re: Call a MEX function from Matlab Engine?
- From: James Tursa
- Call a MEX function from Matlab Engine?
- Prev by Date: Re: taking antilog of an image
- Next by Date: CREATE A PDF OF A GUI SCREEN
- Previous by thread: Re: Call a MEX function from Matlab Engine?
- Next by thread: Re: Call a MEX function from Matlab Engine?
- Index(es):
Relevant Pages
|