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 17:57:03 +0000 (UTC)
Organization: Boeing
Lines: 42
Message-ID: <frh2lf$i59$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> <frftgn$630$1@fred.mathworks.com> <frgbgs$hn8$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1205603823 18601 172.30.248.35 (15 Mar 2008 17:57:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 15 Mar 2008 17:57:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:457406



"David Doria" <daviddoria@gmail.com> wrote in message
<frgbgs$hn8$1@fred.mathworks.com>...
> 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?

No. The MATLAB engine cannot make calls back into your C++
code, and there is no way to call MATLAB functions directly
from within your C++ main application code.

For a mex routine you can use mexCallMATLAB to make direct
calls to MATLAB functions, but there is no equivalent
engCallMATLAB function available. I have written an
engCallMATLAB function to do this (see FEX submission
17494), but this submission merely mimics this capability
with a series of engPutVariable, engEvalString, and
engGetVariable calls. This is the only way to call a MATLAB
function from within your C++ main application.

But I don't understand why you can't simply make a simple
mexFunction front end file, include your entire application,
and make make a mex routine out if it that way. Don't worry
about having a bunch of extra stuff (some of which, like the
console i/o, won't even work in a mex routine) in this file,
since you won't be using that part of the code anyway. Don't
worry about stripping out only those functions you need.
Just put the mexFunction up front and call your desired
functions and return a plhs[0] appropriately and let the
other stuff be dead code. It seems like this would work just
fine with a minimum amount of work on your part. Are you
saying that somehow your mex function needs to communicate
with your engine application code while it is calculating a
result? Or that your engine application code needs to give
the mex function extra inputs that are determined in real
time? Or what? Please clarify.

James Tursa