Path: news.mathworks.com!not-for-mail
From: "Todd Welti" <twelti@harman.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: mex file wrapper for dll needing function handle
Date: Wed, 4 Mar 2009 17:37:02 +0000 (UTC)
Organization: Harman Industries International
Lines: 73
Message-ID: <gome7u$res$1@fred.mathworks.com>
References: <goimue$6j6$1@fred.mathworks.com> <op.up9rjmqpa5ziv5@uthamaa.dhcp.mathworks.com>
Reply-To: "Todd Welti" <twelti@harman.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1236188222 28124 172.30.248.37 (4 Mar 2009 17:37:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 4 Mar 2009 17:37:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 57524
Xref: news.mathworks.com comp.soft-sys.matlab:522505


"Ashish Uthama" <first.last@mathworks.com> wrote in message <op.up9rjmqpa5ziv5@uthamaa.dhcp.mathworks.com>...
> On Tue, 03 Mar 2009 23:11:01 -0500, Todd Welti <twelti@harman.com> wrote:
> 
> > "Ashish Uthama" <first.last@mathworks.com> wrote in message  
> > <op.up8j75y1a5ziv5@uthamaa.dhcp.mathworks.com>...
> >> On Tue, 03 Mar 2009 16:19:03 -0500, James Tursa
> >> <aclassyguywithaknotac@hotmail.com> wrote:
> >>
> >> > "Ashish Uthama" <first.last@mathworks.com> wrote in message
> >> > <op.up8he7c6a5ziv5@uthamaa.dhcp.mathworks.com>...
> >> >>
> >> >> mxCreateScalar will help you convert a double to a mxArray.
> >> >>
> >> >> //air code!
> >> >> mxArray* prhs[3];
> >> >> mwSize dims[2]={ 1 1};
> >> >> prhs[0]=mxCreateNumericArray(2,&dims, mxDOUBLE_CLASS,mxREAL);
> >> >> mexGetPr(prhs[0])=x;
> >> >> //..so on for y and z
> >> >>
> >> >
> >> > Couple of typos. Since dims is an array, it will act as a double *  
> >> when
> >> > passed, so you don't need the &dims in the call, just dims. And you  
> >> are
> >> > missing a comma in the dims initialization. Also, mxGetPr returns a
> >> > pointer result that is not an lvalue. You need to dereference it for  
> >> the
> >> > assignment. e.g.
> >> >
> >> >  mxArray* prhs[3];
> >> >  mwSize dims[2]={ 1, 1};
> >> >  prhs[0]=mxCreateNumericArray(2,dims, mxDOUBLE_CLASS,mxREAL);
> >> >  *(mexGetPr(prhs[0])) = x;
> >> >
> >> > Or, as you almost pointed out, you can use mxCreateDoubleScalar for  
> >> this:
> >> >
> >> > prhs[0] = mxCreateDoubleScalar(x);
> >> >
> >> > James Tursa
> >>
> >>
> >> not typos..but plain errors :)
> >> thanks for the corrections.
> >
> > Why do you ahve to use the *(mexGetPr())?  Isn't prhs already a  
> > pointer?  Why can't you use *prhs[0]?
> 
> The return type of mxCreateNumericArray is mxArray *
> prhs[0] is mxArray *  (think of an mxArray as a structure which represents  
> the MATLAB matrix type in C)
> x is of type double.
> 
> To bridge this gap, use this function:
>      double *mxGetPr(const mxArray *pm);
> 
> mxGetPr(prhs[0]) will give you a double pointer
> *(mxGetPr(prhs[0])) will give you access to the value of this double  
> pointer (the value of MATLAB matrix type). You can then overwrite it with  
> x since the data type now match.
> 
> hth
> 
I think I get it. 
mxArray* prhs is a pointer to an mxArray, not the real data in it.

That would explain why my other attempt:

prhs[0] = &x;

didnt' work either.
Thanks