Path: news.mathworks.com!not-for-mail
From: "David Doria" <daviddoria@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Call a MEX function from Matlab Engine?
Date: Thu, 13 Mar 2008 23:14:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 102
Message-ID: <frccfq$l2f$1@fred.mathworks.com>
References: <frc4bp$ipi$1@fred.mathworks.com> <frc7ir$s19$1@fred.mathworks.com>
Reply-To: "David Doria" <daviddoria@gmail.com>
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 1205450042 21583 172.30.248.38 (13 Mar 2008 23:14:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 13 Mar 2008 23:14:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1105197
Xref: news.mathworks.com comp.soft-sys.matlab:457150



1) sorry, I thought now it was a different question

2) I mentioned that test(4) in matlab outputs 16, but I
actually dont remember now if it returned an int or a
double(i'll check in the morning). I figured that matlab
would convert it to its "matrix" class anyway - does it not?
Would I just change double *myout2; to int *myout2? 
Assuming the function did return an int, in this case it
should be converting from an int to a double, can it not do
that automatically?

2b) If I use mxGetData instead of mxGetPr does that kind of
fix the type automatically?

3) gotcha, I thought I may have to initialize it or
something first, but I guess I was wrong.

4) Yea the engine did open; I plotted some junk before this
to test that.

I'm not a stellar c++ program and this is as far as the
matlab help got me, so I really appreciate the help! 

David


"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in
message <frc7ir$s19$1@fred.mathworks.com>...
> "David Doria" <daviddoria@gmail.com> wrote in message 
> <frc4bp$ipi$1@fred.mathworks.com>...
> > I have a mex file test.mexw32 in
> > 'C:\Documents and Settings\Dave\My Documents\Visual 
> Studio
> > 2005\Projects\Matlab MEX'
> > 
> > My function simply squares the input.  In matlab, test(4)
> > tells me 16.
> > 
> > Now in my c++ code, I did this:
> > 
> > engEvalString(ep, "cd 'C:\Documents and Settings\Dave\My
> > Documents\Visual Studio 2005\Projects\Matlab MEX';");
> > 
> > engEvalString(ep, "a=test(4);");
> > mxArray *test2 = NULL;
> > test2 = mxCreateDoubleMatrix(1, 1, mxREAL);
> > test2 = engGetVariable(ep, "a");
> > double *myout2;
> > myout2 = mxGetPr(test2);
> > cout << myout2[0] << endl;
> > 
> > I get a "Access violation reading location" error. What 
> have
> > I done wrong?
> > 
> > Thanks,
> > 
> > Dave
> 
> Comments:
> 
> 1) You should reply to the original threads in this 
> newsgroup rather than creating new ones. That way people 
> can keep track of the original problem and the various 
> solutions discussed along the way.
> 
> 2) You don't list your mex file, so I don't know what it 
> returns as an output. Does it work? Does it return a 
> double, an integer? Try calling your mex file from MATLAB 
> to verify this, e.g.
> 
> >> test(4)
> >> class(ans)
> 
> If it is not a double class, then of course your mxGetPr 
> call and the follow-on myout2[0] calculation can certainly 
> cause an access violation. Your earlier thread posting 
> indicated your MyFunc returned an integer. Is this what 
> you are using?  In general, you should always check the 
> class, number of elements, etc. of an mxArray before using 
> it.
> 
> 3) The first of these two lines is pointless:
> 
> > test2 = mxCreateDoubleMatrix(1, 1, mxREAL);
> > test2 = engGetVariable(ep, "a");
> 
> Why? Because you first create a brand new mxArray and 
> store a pointer to it in test2. Then you immediately wipe 
> this pointer out with a brand new pointer to the mxArray 
> returned by engGetVariable. This is a memory leak. You 
> just wiped out your only handle to the heap memory 
> allocated for the first double matrix you created. 
> MATLAB's memory manager will fortunately clean this up for 
> you when the mex file exits, but it is still bad.
> 
> 4) I am assuming that the engine opened properly.
> 
> James Tursa
> 
>