Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: Pass by reference in MexFunction Date: Sat, 9 Jul 2011 21:28:09 +0000 (UTC) Organization: Boeing Co Lines: 46 Message-ID: <ivah59$au6$1@newscl01ah.mathworks.com> References: <ibmvr7$9no$1@fred.mathworks.com> <ibnee3$mer$1@fred.mathworks.com> <ibni0g$cud$1@fred.mathworks.com> <ibo39m$pgo$1@fred.mathworks.com> <iv9uu7$rqk$1@newscl01ah.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-00-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1310246889 11206 172.30.248.45 (9 Jul 2011 21:28:09 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sat, 9 Jul 2011 21:28:09 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 756104 Xref: news.mathworks.com comp.soft-sys.matlab:735684 "Amit " <amitsingh.singh@gmail.com> wrote in message <iv9uu7$rqk$1@newscl01ah.mathworks.com>... > "James Tursa" wrote in message <ibo39m$pgo$1@fred.mathworks.com>... > > "Marc Moreau" <jebnor@gmail.com> wrote in message <ibni0g$cud$1@fred.mathworks.com>... > > > "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <ibnee3$mer$1@fred.mathworks.com>... > > > > > > > Arguments to mex routines are pass by reference and can (if you are careful) be modified in-place. Is that what you mean? > > > > > > Yes, that is exactly what I mean. Thank you. > > > > > > As a follow up, in the interest of keeping the interface to the function I am replicating as close as possible to the existing, I'd like to call myfunc() like this: > > > > > > bigdata = myfunc( bigdata ); > > > > > > Is it well defined within MATLAB for my myfunc() to get the data pointer from the rhs[0], manipulate it and then set the lhs[0] data pointer to the same underlying data? > > > > Usually if you set an output variable, e.g. plhs[0], in a mex function MATLAB makes a shared data copy to return. The above *may* work, but that is not how I would do it. My advice is to simply call it like this: > > > > myfunc( bigdata ); > > > > Then modify bigdata in-place and don't set plhs[0] to anything ... just leave it alone. > > > > James Tursa > > Hi, > I want to do exactly same as you have suggested for my problem. But I dont know how to modify input data prhs[0] in place and leave plhs[0] alone. please explain by any simple example. > Thanks in advance, > Amit Call it like this: myfunc( bigdata ); And then the mex function myfunc.c would look like this: #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *pr; pr = mxGetPr(prhs[0]); pr[0] = 99.0; } So the short example above changes the first element of bigdata to 99 in-place. James Tursa