Re: Call a MEX function from Matlab Engine?



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:
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


.



Relevant Pages

  • Re: Call a MEX function from Matlab Engine?
    ... void mexFunction(int nlhs, mxArray *plhs, ... engine, creates a function handle to the mex function ...
    (comp.soft-sys.matlab)
  • Re: Proposal: How to deal with Spaceman
    ... James Driscoll has bombarded the sci.physics.* ... | His pistons will force oil back and forth between two chambers, ... Looks to me like a conventional 2-stroke engine with oil or hydraulic fluid ... | fundamental objection to the principles of operation. ...
    (sci.physics)
  • Re: Proposal: How to deal with Spaceman
    ... James Driscoll has bombarded the sci.physics.* ... | His pistons will force oil back and forth between two chambers, ... Looks to me like a conventional 2-stroke engine with oil or hydraulic fluid ... | fundamental objection to the principles of operation. ...
    (sci.physics.relativity)
  • Re: Video Words
    ... The horsepower was invented (by James Watt) for use with steam ... He was in the business of replacing horses with them. ... you bought a 3-hp engine, you could be sure it would do the job. ...
    (alt.usage.english)
  • Re: After the WHR - what next?
    ... No no no. James the Red Engine, ... There is/was a real loco painted red as James on the Mid Hants. ... A railway which has no representitive examples of its practice ... preserved (one Pug, from a railway which shunned Pugs). ...
    (uk.railway)