Pass pointer to scalar variable in mex function
    5 views (last 30 days)
  
       Show older comments
    
I am having trouble passing a pointer to a scalar variable in a MEX function. The ability to modify variables in the C function without copy-write overhead is highly desirable to me. I have never had trouble passing pointers to vectors using mxGetPr(), but mxGetScalar() seems to only make a copy and modify the copy in the MEX function. The only way I've gotten around this before is by making the scalar variable into a vector and just using the first element, but this is pretty silly.
Another user had a similar question here : https://www.mathworks.com/matlabcentral/answers/306211-in-mex-file-i-can-t-overwrite-scalar-value-through-the-pointer-in-matlab2016a-although-i-can-overw but I do not see if a solution was offered (using an early MATLAB version is not possible, as I need more recent capabilities, such as declaring more data types).
So, if I wanted to get a pointer to the scalar variable 'kijc' in the following, how would I modify the code? I am compiling with R2018a.
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    /* INPUTS */
    double   *Ctst1         =  mxGetPr(prhs[0]);
    double   *Ctst0         =  mxGetPr(prhs[1]);
    double    kijc          =  mxGetScalar(prhs[2]);
    int32_t   cind1         =  mxGetScalar(prhs[3]);
    int32_t   cind2         =  mxGetScalar(prhs[4]);
    double    Qijc0         =  mxGetInt32s(prhs[5]);
    double    Pdif          =  mxGetLogicals(prhs[6]);
    int32_t   numinterdiff  =  mxGetScalar(prhs[7]);
    /* run myfunc */
    myfunc(Ctst1,Ctst0,kijc,cind1,cind2,Qijc0,Pdif,numinterdiff); // modifies kijc
}
1 Comment
  James Tursa
      
      
 on 22 Aug 2019
				
      Edited: James Tursa
      
      
 on 22 Aug 2019
  
			What is the signature (i.e., argument types) of myfunc( )?  Which arguments of myfunc( ) are you intending to change in-place?  Is the problem that you can't trust changing a scalar variable in-place in a mex routine will carry back to the caller (per the link you provided)?
Answers (1)
  James Tursa
      
      
 on 22 Aug 2019
        
      Edited: James Tursa
      
      
 on 23 Aug 2019
  
      "... The only way I've gotten around this before is by making the scalar variable into a vector and just using the first element, but this is pretty silly ..."
Yes, I agree, but that is what the newer API forces you to do.  Or maybe make it part of a cell or struct variable would work also.  Regardless, that's extra work at both ends of the processing caused by what IMO can only have been a very minor performance gain at the MATLAB level.
You should read the following post ... you may be in trouble with your non-scalar variables also.
If you know which variables are scalars (or just small) in advance, it would probably be best to just create new variables inside the mex routine, pass them out, and assign them to the variables you want at the m-file level.
0 Comments
See Also
Categories
				Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
