Path: news.mathworks.com!not-for-mail
From: "Ashish Uthama" <first.last@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: mex file wrapper for dll needing function handle
Date: Wed, 04 Mar 2009 08:24:48 -0500
Organization: TMW
Lines: 64
Message-ID: <op.up9rjmqpa5ziv5@uthamaa.dhcp.mathworks.com>
References: <goimue$6j6$1@fred.mathworks.com>
 <op.up8j75y1a5ziv5@uthamaa.dhcp.mathworks.com>
 <gokv0l$mua$1@fred.mathworks.com>
NNTP-Posting-Host: uthamaa.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15
Content-Transfer-Encoding: 7bit
X-Trace: fred.mathworks.com 1236173088 7021 172.31.57.126 (4 Mar 2009 13:24:48 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 4 Mar 2009 13:24:48 +0000 (UTC)
User-Agent: Opera Mail/9.63 (Win32)
Xref: news.mathworks.com comp.soft-sys.matlab:522424


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