<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645</link>
    <title>MATLAB Central Newsreader - Call a MEX function from Matlab Engine?</title>
    <description>Feed for thread: Call a MEX function from Matlab Engine?</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>Thu, 13 Mar 2008 20:55:21 -0400</pubDate>
      <title>Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#420796</link>
      <author>David Doria</author>
      <description>I have a mex file test.mexw32 in&lt;br&gt;
'C:\Documents and Settings\Dave\My Documents\Visual Studio&lt;br&gt;
2005\Projects\Matlab MEX'&lt;br&gt;
&lt;br&gt;
My function simply squares the input.  In matlab, test(4)&lt;br&gt;
tells me 16.&lt;br&gt;
&lt;br&gt;
Now in my c++ code, I did this:&lt;br&gt;
&lt;br&gt;
engEvalString(ep, &quot;cd 'C:\Documents and Settings\Dave\My&lt;br&gt;
Documents\Visual Studio 2005\Projects\Matlab MEX';&quot;);&lt;br&gt;
&lt;br&gt;
engEvalString(ep, &quot;a=test(4);&quot;);&lt;br&gt;
mxArray *test2 = NULL;&lt;br&gt;
test2 = mxCreateDoubleMatrix(1, 1, mxREAL);&lt;br&gt;
test2 = engGetVariable(ep, &quot;a&quot;);&lt;br&gt;
double *myout2;&lt;br&gt;
myout2 = mxGetPr(test2);&lt;br&gt;
cout &amp;lt;&amp;lt; myout2[0] &amp;lt;&amp;lt; endl;&lt;br&gt;
&lt;br&gt;
I get a &quot;Access violation reading location&quot; error. What have&lt;br&gt;
I done wrong?&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
&lt;br&gt;
Dave</description>
    </item>
    <item>
      <pubDate>Thu, 13 Mar 2008 21:50:19 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#420801</link>
      <author>James Tursa</author>
      <description>&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;frc4bp$ipi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I have a mex file test.mexw32 in&lt;br&gt;
&amp;gt; 'C:\Documents and Settings\Dave\My Documents\Visual &lt;br&gt;
Studio&lt;br&gt;
&amp;gt; 2005\Projects\Matlab MEX'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My function simply squares the input.  In matlab, test(4)&lt;br&gt;
&amp;gt; tells me 16.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now in my c++ code, I did this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; engEvalString(ep, &quot;cd 'C:\Documents and Settings\Dave\My&lt;br&gt;
&amp;gt; Documents\Visual Studio 2005\Projects\Matlab MEX';&quot;);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; engEvalString(ep, &quot;a=test(4);&quot;);&lt;br&gt;
&amp;gt; mxArray *test2 = NULL;&lt;br&gt;
&amp;gt; test2 = mxCreateDoubleMatrix(1, 1, mxREAL);&lt;br&gt;
&amp;gt; test2 = engGetVariable(ep, &quot;a&quot;);&lt;br&gt;
&amp;gt; double *myout2;&lt;br&gt;
&amp;gt; myout2 = mxGetPr(test2);&lt;br&gt;
&amp;gt; cout &amp;lt;&amp;lt; myout2[0] &amp;lt;&amp;lt; endl;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I get a &quot;Access violation reading location&quot; error. What &lt;br&gt;
have&lt;br&gt;
&amp;gt; I done wrong?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Dave&lt;br&gt;
&lt;br&gt;
Comments:&lt;br&gt;
&lt;br&gt;
1) You should reply to the original threads in this &lt;br&gt;
newsgroup rather than creating new ones. That way people &lt;br&gt;
can keep track of the original problem and the various &lt;br&gt;
solutions discussed along the way.&lt;br&gt;
&lt;br&gt;
2) You don't list your mex file, so I don't know what it &lt;br&gt;
returns as an output. Does it work? Does it return a &lt;br&gt;
double, an integer? Try calling your mex file from MATLAB &lt;br&gt;
to verify this, e.g.&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; test(4)&lt;br&gt;
&amp;gt;&amp;gt; class(ans)&lt;br&gt;
&lt;br&gt;
If it is not a double class, then of course your mxGetPr &lt;br&gt;
call and the follow-on myout2[0] calculation can certainly &lt;br&gt;
cause an access violation. Your earlier thread posting &lt;br&gt;
indicated your MyFunc returned an integer. Is this what &lt;br&gt;
you are using?  In general, you should always check the &lt;br&gt;
class, number of elements, etc. of an mxArray before using &lt;br&gt;
it.&lt;br&gt;
&lt;br&gt;
3) The first of these two lines is pointless:&lt;br&gt;
&lt;br&gt;
&amp;gt; test2 = mxCreateDoubleMatrix(1, 1, mxREAL);&lt;br&gt;
&amp;gt; test2 = engGetVariable(ep, &quot;a&quot;);&lt;br&gt;
&lt;br&gt;
Why? Because you first create a brand new mxArray and &lt;br&gt;
store a pointer to it in test2. Then you immediately wipe &lt;br&gt;
this pointer out with a brand new pointer to the mxArray &lt;br&gt;
returned by engGetVariable. This is a memory leak. You &lt;br&gt;
just wiped out your only handle to the heap memory &lt;br&gt;
allocated for the first double matrix you created. &lt;br&gt;
MATLAB's memory manager will fortunately clean this up for &lt;br&gt;
you when the mex file exits, but it is still bad.&lt;br&gt;
&lt;br&gt;
4) I am assuming that the engine opened properly.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Thu, 13 Mar 2008 23:14:02 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#420812</link>
      <author>David Doria</author>
      <description>1) sorry, I thought now it was a different question&lt;br&gt;
&lt;br&gt;
2) I mentioned that test(4) in matlab outputs 16, but I&lt;br&gt;
actually dont remember now if it returned an int or a&lt;br&gt;
double(i'll check in the morning). I figured that matlab&lt;br&gt;
would convert it to its &quot;matrix&quot; class anyway - does it not?&lt;br&gt;
Would I just change double *myout2; to int *myout2? &lt;br&gt;
Assuming the function did return an int, in this case it&lt;br&gt;
should be converting from an int to a double, can it not do&lt;br&gt;
that automatically?&lt;br&gt;
&lt;br&gt;
2b) If I use mxGetData instead of mxGetPr does that kind of&lt;br&gt;
fix the type automatically?&lt;br&gt;
&lt;br&gt;
3) gotcha, I thought I may have to initialize it or&lt;br&gt;
something first, but I guess I was wrong.&lt;br&gt;
&lt;br&gt;
4) Yea the engine did open; I plotted some junk before this&lt;br&gt;
to test that.&lt;br&gt;
&lt;br&gt;
I'm not a stellar c++ program and this is as far as the&lt;br&gt;
matlab help got me, so I really appreciate the help! &lt;br&gt;
&lt;br&gt;
David&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;James Tursa&quot; &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;frc7ir$s19$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;frc4bp$ipi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I have a mex file test.mexw32 in&lt;br&gt;
&amp;gt; &amp;gt; 'C:\Documents and Settings\Dave\My Documents\Visual &lt;br&gt;
&amp;gt; Studio&lt;br&gt;
&amp;gt; &amp;gt; 2005\Projects\Matlab MEX'&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; My function simply squares the input.  In matlab, test(4)&lt;br&gt;
&amp;gt; &amp;gt; tells me 16.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Now in my c++ code, I did this:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; engEvalString(ep, &quot;cd 'C:\Documents and Settings\Dave\My&lt;br&gt;
&amp;gt; &amp;gt; Documents\Visual Studio 2005\Projects\Matlab MEX';&quot;);&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; engEvalString(ep, &quot;a=test(4);&quot;);&lt;br&gt;
&amp;gt; &amp;gt; mxArray *test2 = NULL;&lt;br&gt;
&amp;gt; &amp;gt; test2 = mxCreateDoubleMatrix(1, 1, mxREAL);&lt;br&gt;
&amp;gt; &amp;gt; test2 = engGetVariable(ep, &quot;a&quot;);&lt;br&gt;
&amp;gt; &amp;gt; double *myout2;&lt;br&gt;
&amp;gt; &amp;gt; myout2 = mxGetPr(test2);&lt;br&gt;
&amp;gt; &amp;gt; cout &amp;lt;&amp;lt; myout2[0] &amp;lt;&amp;lt; endl;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I get a &quot;Access violation reading location&quot; error. What &lt;br&gt;
&amp;gt; have&lt;br&gt;
&amp;gt; &amp;gt; I done wrong?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Thanks,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Dave&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Comments:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 1) You should reply to the original threads in this &lt;br&gt;
&amp;gt; newsgroup rather than creating new ones. That way people &lt;br&gt;
&amp;gt; can keep track of the original problem and the various &lt;br&gt;
&amp;gt; solutions discussed along the way.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 2) You don't list your mex file, so I don't know what it &lt;br&gt;
&amp;gt; returns as an output. Does it work? Does it return a &lt;br&gt;
&amp;gt; double, an integer? Try calling your mex file from MATLAB &lt;br&gt;
&amp;gt; to verify this, e.g.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; test(4)&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; class(ans)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If it is not a double class, then of course your mxGetPr &lt;br&gt;
&amp;gt; call and the follow-on myout2[0] calculation can certainly &lt;br&gt;
&amp;gt; cause an access violation. Your earlier thread posting &lt;br&gt;
&amp;gt; indicated your MyFunc returned an integer. Is this what &lt;br&gt;
&amp;gt; you are using?  In general, you should always check the &lt;br&gt;
&amp;gt; class, number of elements, etc. of an mxArray before using &lt;br&gt;
&amp;gt; it.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 3) The first of these two lines is pointless:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; test2 = mxCreateDoubleMatrix(1, 1, mxREAL);&lt;br&gt;
&amp;gt; &amp;gt; test2 = engGetVariable(ep, &quot;a&quot;);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Why? Because you first create a brand new mxArray and &lt;br&gt;
&amp;gt; store a pointer to it in test2. Then you immediately wipe &lt;br&gt;
&amp;gt; this pointer out with a brand new pointer to the mxArray &lt;br&gt;
&amp;gt; returned by engGetVariable. This is a memory leak. You &lt;br&gt;
&amp;gt; just wiped out your only handle to the heap memory &lt;br&gt;
&amp;gt; allocated for the first double matrix you created. &lt;br&gt;
&amp;gt; MATLAB's memory manager will fortunately clean this up for &lt;br&gt;
&amp;gt; you when the mex file exits, but it is still bad.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 4) I am assuming that the engine opened properly.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; James Tursa&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; </description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 00:34:01 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#420818</link>
      <author>James Tursa</author>
      <description>&lt;br&gt;
I don't know if you need this much advice, but here &lt;br&gt;
goes ...&lt;br&gt;
&lt;br&gt;
&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;frccfq$l2f$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 2) I mentioned that test(4) in matlab outputs 16, but I&lt;br&gt;
&amp;gt; actually dont remember now if it returned an int or a&lt;br&gt;
&amp;gt; double(i'll check in the morning). I figured that matlab&lt;br&gt;
&amp;gt; would convert it to its &quot;matrix&quot; class anyway - does it &lt;br&gt;
not?&lt;br&gt;
&lt;br&gt;
There is no such thing as a &quot;matrix&quot; class. The class &lt;br&gt;
types are things &lt;br&gt;
like &quot;double&quot;, &quot;single&quot;, &quot;int32&quot;, &quot;int16&quot;, etc. Variables &lt;br&gt;
of these class types can be scalars, vectors, matrices, or &lt;br&gt;
multi-dimensional arrays. If you type in a constant &lt;br&gt;
number, like the argument 4 in your test(4) call, MATLAB &lt;br&gt;
will convert the 4 into an mxArray of type double class, &lt;br&gt;
with dimensions 1 x 1 (i.e. scalar). This is what gets &lt;br&gt;
passed to the test function. If test is a mex function, &lt;br&gt;
then that means that prhs[0] will be a pointer to an &lt;br&gt;
mxArray of type double with one element. There is no &lt;br&gt;
automatic conversion as far as accessing this data is &lt;br&gt;
concerned. It is a 64-bit floating point value and needs &lt;br&gt;
to be treated as such in your C++ code. There are various &lt;br&gt;
ways to get the scalar value for use in your C++ code:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double d;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double *dp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dp = mxGetPr(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d = *dp;&lt;br&gt;
&lt;br&gt;
or&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double d;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double *dp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dp = (double *) mxGetData(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d = *dp;&lt;br&gt;
&lt;br&gt;
or&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double d;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d = *(mxGetPr(prhs[0]));&lt;br&gt;
&lt;br&gt;
If you know the input has an integer value and want to use &lt;br&gt;
it as an int, you can use a cast to do this, for instance&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int i;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i = (int) *(mxGetPr(prhs[0]));&lt;br&gt;
&lt;br&gt;
but you CANNOT do something like this&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int i;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int *ip;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ip = mxGetPr(prhs[0]); // bad bad bad&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i = *ip;&lt;br&gt;
&lt;br&gt;
and the use of a cast doesn't fix this either&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int i;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int *ip;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ip = (int *) mxGetPr(prhs[0]); // bad bad bad&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i = *ip;&lt;br&gt;
&lt;br&gt;
These last two cases takes the result of mxGetPr, which is &lt;br&gt;
always a pointer to double, and then subsequently treats &lt;br&gt;
it as a pointer to int when dereferencing. Since the &lt;br&gt;
underlying bits are 64-bit floating point this will give &lt;br&gt;
you garbage.&lt;br&gt;
&lt;br&gt;
Same comments apply to the output of your mex function. &lt;br&gt;
plhs[0] is created by you with whatever class type you &lt;br&gt;
select. If you use mxCreateDoubleMatrix, for instance, &lt;br&gt;
then the class will be double and the data will be 64-bit &lt;br&gt;
floating point and must be treated as such. If your C++ &lt;br&gt;
code returns an int that you want stuffed into plhs[0], &lt;br&gt;
then you would have to make sure it was cast either &lt;br&gt;
explicitly or implicitly. For example suppose MyFunc &lt;br&gt;
returns an int:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int i;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;plhs[0] = mxCreateDoubleMatrix( 1, 1, mxREAL );&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i = MyFunc( blah blah );&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*(mxGetPr(plhs[0])) = (double) i;&lt;br&gt;
&lt;br&gt;
Here we are getting an int result from MyFunc, casting &lt;br&gt;
that result into a double, and then putting that into the &lt;br&gt;
data area of plhs[0]. You don't actually *need* the &lt;br&gt;
(double) cast in this assignment since it will happen &lt;br&gt;
automatically in this case, but I think it is good &lt;br&gt;
practice to put these in your code to make them obvious to &lt;br&gt;
the reader what is going on.&lt;br&gt;
&lt;br&gt;
&amp;gt; Would I just change double *myout2; to int *myout2? &lt;br&gt;
&amp;gt; Assuming the function did return an int, in this case it&lt;br&gt;
&amp;gt; should be converting from an int to a double, can it not &lt;br&gt;
do&lt;br&gt;
&amp;gt; that automatically?&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
See the above comments. You can certainly cast a double to &lt;br&gt;
an int, but you cannot cast a pointer to double into a &lt;br&gt;
pointer to int and then expect to get a correct answer &lt;br&gt;
when dereferencing it.&lt;br&gt;
&lt;br&gt;
I suggest you post your mex code so that I can look at it &lt;br&gt;
and see what you are doing.&lt;br&gt;
&lt;br&gt;
&amp;gt; 2b) If I use mxGetData instead of mxGetPr does that kind &lt;br&gt;
of&lt;br&gt;
&amp;gt; fix the type automatically?&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
No. mxGetData returns a pointer to void, whereas mxGetPr &lt;br&gt;
returns a pointer to double, but they both return the same &lt;br&gt;
value. i.e., the address they return is the same, namely &lt;br&gt;
the starting address of the data.  mxGetPr should only be &lt;br&gt;
used with mxArrays of class double, and mxGetData should &lt;br&gt;
be used for all other classes (single, int32, etc.) and &lt;br&gt;
then a cast used ((float *), (int *), etc.) on the result &lt;br&gt;
to get proper access to the data.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 13:51:02 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#420902</link>
      <author>David Doria</author>
      <description>ok, I tried again to no avail...&lt;br&gt;
I understand the stuff about the types and the pointer types&lt;br&gt;
now, thanks for the explanation. I am clearly still missing&lt;br&gt;
something else though.&lt;br&gt;
&lt;br&gt;
In matlab, mexfunc(4) sets ans to 8.  class(ans) is double.&lt;br&gt;
&lt;br&gt;
Here is the MEX code: (mexfunc.cpp)&lt;br&gt;
&lt;br&gt;
#include &quot;mex.h&quot;&lt;br&gt;
&lt;br&gt;
void timestwo(double y[], double x[])&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;y[0] = 2*x[0];&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
void mexFunction( int nlhs, mxArray *plhs[],&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int nrhs, const mxArray *prhs[] )&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;double *x,*y;&lt;br&gt;
&amp;nbsp;&amp;nbsp;mwSize mrows,ncols;&lt;br&gt;
&amp;nbsp;&amp;nbsp;plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);&lt;br&gt;
&amp;nbsp;&amp;nbsp;x = mxGetPr(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;y = mxGetPr(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;timestwo(y,x);&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
And here is the c++ code:&lt;br&gt;
&lt;br&gt;
engEvalString(ep, &quot;cd 'C:\Documents and Settings\Dave\My&lt;br&gt;
Documents\Visual Studio 2005\Projects\Matlab MEX';&quot;);&lt;br&gt;
&lt;br&gt;
engEvalString(ep, &quot;a=mexfunc(4);&quot;);&lt;br&gt;
mxArray *test2 = NULL;&lt;br&gt;
test2 = engGetVariable(ep, &quot;a&quot;);&lt;br&gt;
double *myout2;&lt;br&gt;
myout2 = mxGetPr(test2); //Access violation on this line&lt;br&gt;
double d;&lt;br&gt;
d=*myout2;&lt;br&gt;
cout &amp;lt;&amp;lt; d &amp;lt;&amp;lt; endl;&lt;br&gt;
&lt;br&gt;
Any more clues for me? haha&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
Dave</description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 14:45:04 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#420914</link>
      <author>James Tursa</author>
      <description>&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;frdvs6$lim$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any more clues for me? haha&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; Dave&lt;br&gt;
&lt;br&gt;
This worked for me. I suspect that your MATLAB engine cannot&lt;br&gt;
see your test function, so when you try to execute it and&lt;br&gt;
access the answer you get garbage. Print out the values of&lt;br&gt;
all the pointers before you use them to verify this. Is test&lt;br&gt;
on the path where the MATLAB engine can see it?&lt;br&gt;
&lt;br&gt;
James Tursa&lt;br&gt;
&lt;br&gt;
#include &amp;lt;iostream&amp;gt;&lt;br&gt;
#include &quot;engine.h&quot;&lt;br&gt;
&lt;br&gt;
using namespace std;&lt;br&gt;
&lt;br&gt;
int main()&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Engine *ep;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ep = engOpen(NULL);&lt;br&gt;
&lt;br&gt;
//engEvalString(ep, &quot;cd 'C:\Documents and&lt;br&gt;
Settings\Dave\MyDocuments\Visual Studio 2005\Projects\Matlab&lt;br&gt;
MEX';&quot;);&lt;br&gt;
&lt;br&gt;
engEvalString(ep, &quot;a=mexfunc(4);&quot;);&lt;br&gt;
mxArray *test2 = NULL;&lt;br&gt;
test2 = engGetVariable(ep, &quot;a&quot;);&lt;br&gt;
double *myout2;&lt;br&gt;
myout2 = mxGetPr(test2); //Access violation on this line&lt;br&gt;
double d;&lt;br&gt;
d=*myout2;&lt;br&gt;
cout &amp;lt;&amp;lt; d &amp;lt;&amp;lt; endl;&lt;br&gt;
&lt;br&gt;
}</description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 15:41:03 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#420921</link>
      <author>David Doria</author>
      <description>As you guessed, test2 was 00000000.  For some reason my&lt;br&gt;
compiler was completely skipping the line about &quot;cd the&lt;br&gt;
directory&quot;.  I removed the couple of lines before this code&lt;br&gt;
that i plotted something just to test if the engine opened&lt;br&gt;
properly and the compiler complained that I had used \&lt;br&gt;
instead of / in the path... Why it ignored it with the plot&lt;br&gt;
command I have NO idea.&lt;br&gt;
&lt;br&gt;
But it worked for me now as well.  So now back to my first&lt;br&gt;
question of how to call fminunc() from the engine.  I'm not&lt;br&gt;
sure if this is exactly what you were suggesting, but I&lt;br&gt;
think it will work:&lt;br&gt;
&lt;br&gt;
1) I have test.cpp which contains f() which I intend to&lt;br&gt;
minimize:&lt;br&gt;
//for now I made it extra simple&lt;br&gt;
double f()&lt;br&gt;
{&lt;br&gt;
	return 2.81;&lt;br&gt;
}&lt;br&gt;
2) I have mexfunc.cpp which contains mexFunction() which I&lt;br&gt;
can now get the value from in main() in test.cpp&lt;br&gt;
3) All thats left to do is call f() from mexFunction().  I&lt;br&gt;
made a test.h and all I put in it is:&lt;br&gt;
double f();&lt;br&gt;
&lt;br&gt;
then in mexfunc.cpp I put:&lt;br&gt;
#include &quot;test.h&quot;&lt;br&gt;
&lt;br&gt;
Then I try to run 'mex mexfunc.cpp' in matlab and I get:&lt;br&gt;
&lt;br&gt;
mexfunc.obj : error LNK2019: unresolved external symbol&lt;br&gt;
&quot;double __cdecl f(void)&quot; (?f@@YANXZ) referenced in function&lt;br&gt;
_mexFunction &lt;br&gt;
&lt;br&gt;
Any thoughts? I promise (well maybe not!) I'll leave you&lt;br&gt;
alone after this :)&lt;br&gt;
&lt;br&gt;
Dave</description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 15:51:02 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#420923</link>
      <author>John reilly</author>
      <description>I'll tell you why it ignored your &quot;cd&quot; command:  you're&lt;br&gt;
using C/C++ and your string contained &quot;\&quot;.  This is an&lt;br&gt;
escape character in C strings.  If you want your path to be&lt;br&gt;
passed to MATLAB, you need to escape the escape character:&lt;br&gt;
&lt;br&gt;
engEvalString(ep, &quot;cd 'C:\\Documents and Settings\\Dave\\My&lt;br&gt;
Documents\\Visual Studio 2005\\Projects\\Matlab MEX';&quot;);&lt;br&gt;
&lt;br&gt;
oops.</description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 16:18:01 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#420931</link>
      <author>David Doria</author>
      <description>Right, but I'm saying that with the escape characters in the&lt;br&gt;
string the compiler actually complained that D and D and M&lt;br&gt;
and V and P and M were not valid escape characters.  &lt;br&gt;
&lt;br&gt;
When I had some other matlab stuff in my c++ code WITH the&lt;br&gt;
escape characters, the compiler didnt complain and compiled&lt;br&gt;
fine.  When I removed the other matlab stuff, and still had&lt;br&gt;
the escape characters, it complained about the escape&lt;br&gt;
characters.  See what I'm saying? I no longer have a&lt;br&gt;
question about it because it works not, but it was really odd.&lt;br&gt;
&lt;br&gt;
&quot;John Reilly&quot; &amp;lt;jrhokie1.nospam@yahoo.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fre6t6$rm7$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I'll tell you why it ignored your &quot;cd&quot; command:  you're&lt;br&gt;
&amp;gt; using C/C++ and your string contained &quot;\&quot;.  This is an&lt;br&gt;
&amp;gt; escape character in C strings.  If you want your path to be&lt;br&gt;
&amp;gt; passed to MATLAB, you need to escape the escape character:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; engEvalString(ep, &quot;cd 'C:\\Documents and Settings\\Dave\\My&lt;br&gt;
&amp;gt; Documents\\Visual Studio 2005\\Projects\\Matlab MEX';&quot;);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; oops.&lt;br&gt;
&amp;gt; </description>
    </item>
    <item>
      <pubDate>Sat, 15 Mar 2008 07:23:03 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#421026</link>
      <author>James Tursa</author>
      <description>&lt;br&gt;
Here is a complete working example. Maybe by comparing this&lt;br&gt;
to what you have you can decipher what is wrong with your&lt;br&gt;
code.  Call the following file myfunc.cpp and make sure it&lt;br&gt;
is on the MATLAB path. This is a simple mex function that&lt;br&gt;
computes a quadratic function with obvious min at x = 4.0.&lt;br&gt;
-------------------------------------------------&lt;br&gt;
#include &quot;mex.h&quot;&lt;br&gt;
#include &quot;matrix.h&quot;&lt;br&gt;
&lt;br&gt;
double quadratic(double x)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;return 3.0 + (x - 4.0)*(x - 4.0);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
void mexFunction( int nlhs, mxArray *plhs[],&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int nrhs, const mxArray *prhs[] )&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);&lt;br&gt;
&amp;nbsp;&amp;nbsp;*mxGetPr(plhs[0]) = quadratic(*mxGetPr(prhs[0]));&lt;br&gt;
}&lt;br&gt;
-------------------------------------------------&lt;br&gt;
mex it as follows:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; mex myfunc.cpp&lt;br&gt;
&lt;br&gt;
Then call the following file engfunc.cpp. I used fminsearch&lt;br&gt;
instead of fminunc for this example. This program opens the&lt;br&gt;
engine, creates a function handle to the mex function&lt;br&gt;
myfunc, calls fminsearch to find the minimum of this mex&lt;br&gt;
function starting with a guess of 0, then gets the result&lt;br&gt;
into the C++ code and displays it. I left the engine&lt;br&gt;
running, but of course you could close it if you wanted to.&lt;br&gt;
-------------------------------------------------&lt;br&gt;
#include &amp;lt;iostream&amp;gt;&lt;br&gt;
#include &quot;engine.h&quot;&lt;br&gt;
&lt;br&gt;
using namespace std;&lt;br&gt;
&lt;br&gt;
int main()&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Engine *ep;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mxArray *test2;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ep = engOpen( NULL );&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;engEvalString( ep, &quot;fun = @myfunc;&quot; );&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;engEvalString( ep, &quot;a = fminsearch(fun,0);&quot; );&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if( test2 = engGetVariable( ep, &quot;a&quot; ) )&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; *mxGetPr( test2 ) &amp;lt;&amp;lt; endl;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mxDestroyArray( test2 );&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; &quot;Didn't work&quot; &amp;lt;&amp;lt; endl;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
}&lt;br&gt;
-------------------------------------------------&lt;br&gt;
I compiled it as follows for VC++ 8.0:&lt;br&gt;
&amp;gt;&amp;gt; options = [matlabroot&lt;br&gt;
'\bin\win32\mexopts\msvc80engmatopts.bat'];&lt;br&gt;
&amp;gt;&amp;gt; mex('-f', options, 'engfunc.cpp', '-v');&lt;br&gt;
&lt;br&gt;
You will of course have to modify this for your particular&lt;br&gt;
compiler.&lt;br&gt;
&lt;br&gt;
Then you can execute it as follows to find the minimum of&lt;br&gt;
myfunc and display it to the screen from your engine app:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; !engfunc&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4&lt;br&gt;
&lt;br&gt;
I haven't included any error checking in the code to keep&lt;br&gt;
things simple for the example. Well, no, actually the real&lt;br&gt;
reason is that it is late and I am tired and just lazy, but&lt;br&gt;
pretend the first reason I just gave is the correct one.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Sat, 15 Mar 2008 11:22:04 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#421038</link>
      <author>David Doria</author>
      <description>James, thanks for the example.  However, I was able to get&lt;br&gt;
that way working.  What I need is for quadratic(double x) to&lt;br&gt;
be in engfunc.cpp.  This is my &quot;main&quot; program in which the&lt;br&gt;
cost function relies on many other functions - I cannot&lt;br&gt;
transfer them all to myfunc.cpp.&lt;br&gt;
&lt;br&gt;
Is this possible?&lt;br&gt;
&lt;br&gt;
&quot;James Tursa&quot; &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;frftgn$630$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Here is a complete working example. Maybe by comparing this&lt;br&gt;
&amp;gt; to what you have you can decipher what is wrong with your&lt;br&gt;
&amp;gt; code.  Call the following file myfunc.cpp and make sure it&lt;br&gt;
&amp;gt; is on the MATLAB path. This is a simple mex function that&lt;br&gt;
&amp;gt; computes a quadratic function with obvious min at x = 4.0.&lt;br&gt;
&amp;gt; -------------------------------------------------&lt;br&gt;
&amp;gt; #include &quot;mex.h&quot;&lt;br&gt;
&amp;gt; #include &quot;matrix.h&quot;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; double quadratic(double x)&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;   return 3.0 + (x - 4.0)*(x - 4.0);&lt;br&gt;
&amp;gt; }&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; void mexFunction( int nlhs, mxArray *plhs[],&lt;br&gt;
&amp;gt;                   int nrhs, const mxArray *prhs[] )&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;   plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);&lt;br&gt;
&amp;gt;   *mxGetPr(plhs[0]) = quadratic(*mxGetPr(prhs[0]));&lt;br&gt;
&amp;gt; }&lt;br&gt;
&amp;gt; -------------------------------------------------&lt;br&gt;
&amp;gt; mex it as follows:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; mex myfunc.cpp&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Then call the following file engfunc.cpp. I used fminsearch&lt;br&gt;
&amp;gt; instead of fminunc for this example. This program opens the&lt;br&gt;
&amp;gt; engine, creates a function handle to the mex function&lt;br&gt;
&amp;gt; myfunc, calls fminsearch to find the minimum of this mex&lt;br&gt;
&amp;gt; function starting with a guess of 0, then gets the result&lt;br&gt;
&amp;gt; into the C++ code and displays it. I left the engine&lt;br&gt;
&amp;gt; running, but of course you could close it if you wanted to.&lt;br&gt;
&amp;gt; -------------------------------------------------&lt;br&gt;
&amp;gt; #include &amp;lt;iostream&amp;gt;&lt;br&gt;
&amp;gt; #include &quot;engine.h&quot;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; using namespace std;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; int main()&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;     Engine *ep;&lt;br&gt;
&amp;gt;     mxArray *test2;&lt;br&gt;
&amp;gt;     &lt;br&gt;
&amp;gt;     ep = engOpen( NULL );&lt;br&gt;
&amp;gt;     engEvalString( ep, &quot;fun = @myfunc;&quot; );&lt;br&gt;
&amp;gt;     engEvalString( ep, &quot;a = fminsearch(fun,0);&quot; );&lt;br&gt;
&amp;gt;     if( test2 = engGetVariable( ep, &quot;a&quot; ) )&lt;br&gt;
&amp;gt;     {&lt;br&gt;
&amp;gt;         cout &amp;lt;&amp;lt; *mxGetPr( test2 ) &amp;lt;&amp;lt; endl;&lt;br&gt;
&amp;gt;         mxDestroyArray( test2 );&lt;br&gt;
&amp;gt;     }&lt;br&gt;
&amp;gt;     else&lt;br&gt;
&amp;gt;     {&lt;br&gt;
&amp;gt;         cout &amp;lt;&amp;lt; &quot;Didn't work&quot; &amp;lt;&amp;lt; endl;&lt;br&gt;
&amp;gt;     }&lt;br&gt;
&amp;gt; }&lt;br&gt;
&amp;gt; -------------------------------------------------&lt;br&gt;
&amp;gt; I compiled it as follows for VC++ 8.0:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; options = [matlabroot&lt;br&gt;
&amp;gt; '\bin\win32\mexopts\msvc80engmatopts.bat'];&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; mex('-f', options, 'engfunc.cpp', '-v');&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You will of course have to modify this for your particular&lt;br&gt;
&amp;gt; compiler.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Then you can execute it as follows to find the minimum of&lt;br&gt;
&amp;gt; myfunc and display it to the screen from your engine app:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; !engfunc&lt;br&gt;
&amp;gt;     4&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I haven't included any error checking in the code to keep&lt;br&gt;
&amp;gt; things simple for the example. Well, no, actually the real&lt;br&gt;
&amp;gt; reason is that it is late and I am tired and just lazy, but&lt;br&gt;
&amp;gt; pretend the first reason I just gave is the correct one.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; James Tursa&lt;br&gt;
&amp;gt; </description>
    </item>
    <item>
      <pubDate>Sat, 15 Mar 2008 17:57:03 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#421067</link>
      <author>James Tursa</author>
      <description>&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;frgbgs$hn8$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; James, thanks for the example.  However, I was able to get&lt;br&gt;
&amp;gt; that way working.  What I need is for quadratic(double x) to&lt;br&gt;
&amp;gt; be in engfunc.cpp.  This is my &quot;main&quot; program in which the&lt;br&gt;
&amp;gt; cost function relies on many other functions - I cannot&lt;br&gt;
&amp;gt; transfer them all to myfunc.cpp.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Is this possible?&lt;br&gt;
&lt;br&gt;
No. The MATLAB engine cannot make calls back into your C++&lt;br&gt;
code, and there is no way to call MATLAB functions directly&lt;br&gt;
from within your C++ main application code.&lt;br&gt;
&lt;br&gt;
For a mex routine you can use mexCallMATLAB to make direct&lt;br&gt;
calls to MATLAB functions, but there is no equivalent&lt;br&gt;
engCallMATLAB function available. I have written an&lt;br&gt;
engCallMATLAB function to do this (see FEX submission&lt;br&gt;
17494), but this submission merely mimics this capability&lt;br&gt;
with a series of engPutVariable, engEvalString, and&lt;br&gt;
engGetVariable calls. This is the only way to call a MATLAB&lt;br&gt;
function from within your C++ main application.&lt;br&gt;
&lt;br&gt;
But I don't understand why you can't simply make a simple&lt;br&gt;
mexFunction front end file, include your entire application,&lt;br&gt;
and make make a mex routine out if it that way. Don't worry&lt;br&gt;
about having a bunch of extra stuff (some of which, like the&lt;br&gt;
console i/o, won't even work in a mex routine) in this file,&lt;br&gt;
since you won't be using that part of the code anyway. Don't&lt;br&gt;
worry about stripping out only those functions you need.&lt;br&gt;
Just put the mexFunction up front and call your desired&lt;br&gt;
functions and return a plhs[0] appropriately and let the&lt;br&gt;
other stuff be dead code. It seems like this would work just&lt;br&gt;
fine with a minimum amount of work on your part. Are you&lt;br&gt;
saying that somehow your mex function needs to communicate&lt;br&gt;
with your engine application code while it is calculating a&lt;br&gt;
result? Or that your engine application code needs to give&lt;br&gt;
the mex function extra inputs that are determined in real&lt;br&gt;
time? Or what? Please clarify.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Sat, 15 Mar 2008 18:19:01 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#421069</link>
      <author>David Doria</author>
      <description>Sorry I was not clear.  The actual c++ function I want to&lt;br&gt;
minimze is&lt;br&gt;
&lt;br&gt;
double CostFunction();&lt;br&gt;
&lt;br&gt;
CostFunction() calls many other functions such as&lt;br&gt;
DistanceToPoint(), DistanceToPolygon() blah blah and also&lt;br&gt;
records alot of things that are globals for the sake of the&lt;br&gt;
OpenGL portion of my program. Also, these Distance()&lt;br&gt;
functions that I mention rely on alot of classes that I&lt;br&gt;
wrote such as &lt;br&gt;
class Polygon&lt;br&gt;
class Edge&lt;br&gt;
etc&lt;br&gt;
&lt;br&gt;
I don't see how I can mex call of those things.  Maybe I&lt;br&gt;
simply can't do this.  I found a package called GSL (GNU&lt;br&gt;
scientific library) that has some unconstrained&lt;br&gt;
minimization, and also a package for GSL called OOL (Open&lt;br&gt;
Optimization Library) that contains some basic constrained&lt;br&gt;
minimization functions.  These seem to work fine, but I&lt;br&gt;
would have like to confirm their results with the matlab&lt;br&gt;
fminunc() and fmincon() functions.&lt;br&gt;
&lt;br&gt;
Thanks for all your help - at least I learned alot about mex&lt;br&gt;
even if I can't apply it to this problem!&lt;br&gt;
&lt;br&gt;
David</description>
    </item>
    <item>
      <pubDate>Sat, 15 Mar 2008 22:15:07 -0400</pubDate>
      <title>Re: Call a MEX function from Matlab Engine?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165645#421094</link>
      <author>James Tursa</author>
      <description>&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;frh3ul$qi1$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Sorry I was not clear.  The actual c++ function I want to&lt;br&gt;
&amp;gt; minimze is&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; double CostFunction();&lt;br&gt;
&lt;br&gt;
I don't get it. What is the independent variable here? Cost&lt;br&gt;
function based on what independent input(s)? How were you&lt;br&gt;
thinking of calling fminunc or fmincon?&lt;br&gt;
&lt;br&gt;
&amp;gt; CostFunction() calls many other functions such as&lt;br&gt;
&amp;gt; DistanceToPoint(), DistanceToPolygon() blah blah and also&lt;br&gt;
&amp;gt; records alot of things that are globals for the sake of the&lt;br&gt;
&amp;gt; OpenGL portion of my program. Also, these Distance()&lt;br&gt;
&amp;gt; functions that I mention rely on alot of classes that I&lt;br&gt;
&amp;gt; wrote such as &lt;br&gt;
&amp;gt; class Polygon&lt;br&gt;
&amp;gt; class Edge&lt;br&gt;
&amp;gt; etc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I don't see how I can mex call of those things.  Maybe I&lt;br&gt;
&amp;gt; simply can't do this.&lt;br&gt;
&lt;br&gt;
Compiling is not the issue, really. You can use all of your&lt;br&gt;
classes and custom functions in a mex routine. Think of it&lt;br&gt;
this way: A mex routine is a dll with no console i/o&lt;br&gt;
capability that hooks directly into MATLAB as a callable&lt;br&gt;
function, whereas an engine application is a stand-alone&lt;br&gt;
program with full console i/o capability that communicates&lt;br&gt;
with a separate MATLAB process via (I think) a COM link. But&lt;br&gt;
in both cases you still have full access to everything that&lt;br&gt;
a C++ compiler can compile.&lt;br&gt;
&lt;br&gt;
Sounds like you need your mex routine to communicate data&lt;br&gt;
with your engine application (i.e., the globals you mention&lt;br&gt;
and maybe other things). Is this correct? If so, then that&lt;br&gt;
is going to be a problem. We can probably set up something&lt;br&gt;
to transmit fundamental types back &amp; forth (int, double,&lt;br&gt;
etc.), but if you need to transmit class variables back &amp;&lt;br&gt;
forth then that would be a huge issue and probably not worth&lt;br&gt;
the effort.&lt;br&gt;
&lt;br&gt;
&amp;gt; I found a package called GSL (GNU&lt;br&gt;
&amp;gt; scientific library) that has some unconstrained&lt;br&gt;
&amp;gt; minimization, and also a package for GSL called OOL (Open&lt;br&gt;
&amp;gt; Optimization Library) that contains some basic constrained&lt;br&gt;
&amp;gt; minimization functions.  These seem to work fine, but I&lt;br&gt;
&amp;gt; would have like to confirm their results with the matlab&lt;br&gt;
&amp;gt; fminunc() and fmincon() functions.&lt;br&gt;
&lt;br&gt;
If you can set your problem up so that your cost function&lt;br&gt;
fits into something fminunc and fmincon can handle, then&lt;br&gt;
maybe it can be done. But if you have a lot of global class&lt;br&gt;
variable etc that your cost function depends on, then it&lt;br&gt;
will probably be too much work.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
  </channel>
</rss>

