http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554
MATLAB Central Newsreader  rand() using mexCallMATLAB
Feed for thread: rand() using mexCallMATLAB
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 12 Sep 2013 10:47:09 +0000
rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911097
Hariprasad
I want to use the existing Matlab rand() function in a Mex function. I have two int variables which determine the size of the array of random numbers. Since I have to use mexCallMATLAB for using the Matlab rand() function, I don't know how to pass the 2 int variables as the prhs[] input to mexCallMATLAB. Any help will be really helpful.

Thu, 12 Sep 2013 10:53:06 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911098
Hariprasad
"Hariprasad" wrote in message <l0s63d$f2e$1@newscl01ah.mathworks.com>...<br>
> I want to use the existing Matlab rand() function in a Mex function. I have two int variables(rows and cols) which determine the size of the array of random numbers. Since I have to use mexCallMATLAB for using the Matlab rand() function, I don't know how to pass the 2 int variables as the prhs[] input to mexCallMATLAB. Any help will be really helpful.

Thu, 12 Sep 2013 12:20:06 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911102
Hariprasad
"Hariprasad" wrote in message <l0s6ei$krk$1@newscl01ah.mathworks.com>...<br>
> "Hariprasad" wrote in message <l0s63d$f2e$1@newscl01ah.mathworks.com>...<br>
> > I want to use the existing Matlab rand() function in a Mex function. I have two int variables(rows and cols) which determine the size of the array of random numbers. Since I have to use mexCallMATLAB for using the Matlab rand() function, I don't know how to pass the 2 int variables as the prhs[] input to mexCallMATLAB. Any help will be really helpful.<br>
<br>
i have done something like this inside the mexFunction()<br>
<br>
mxArray *lhs,*rhs[2];<br>
int *p1,*p2;<br>
double *randoms;<br>
<br>
lhs = mxCreateDoubleMatrix(m,no_of_levels,mxREAL); /* m =2, no_of_levels = 4 */<br>
randoms = mxGetPr(lhs);<br>
rhs[0] = mxCreateNumericMatrix(1,1,mxUINT8_CLASS,mxREAL);<br>
rhs[1] = mxCreateNumericMatrix(1,1,mxUINT8_CLASS,mxREAL);<br>
p1 = mxGetPr(rhs[0]); *p1 = m;<br>
p2 = mxGetPr(rhs[1]); *p2 = no_of_levels;<br>
<br>
mxCallMATLAB(1,lhs,2,rhs,"rand");<br>
<br>
but the data is zero in randoms. Where am I going wrong??<br>
Thanks

Thu, 12 Sep 2013 14:44:06 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911118
James Tursa
"Hariprasad" wrote in message <l0sbhm$m70$1@newscl01ah.mathworks.com>...<br>
> "Hariprasad" wrote in message <l0s6ei$krk$1@newscl01ah.mathworks.com>...<br>
> > "Hariprasad" wrote in message <l0s63d$f2e$1@newscl01ah.mathworks.com>...<br>
> > > I want to use the existing Matlab rand() function in a Mex function. I have two int variables(rows and cols) which determine the size of the array of random numbers. Since I have to use mexCallMATLAB for using the Matlab rand() function, I don't know how to pass the 2 int variables as the prhs[] input to mexCallMATLAB. Any help will be really helpful.<br>
> <br>
> i have done something like this inside the mexFunction()<br>
> <br>
> mxArray *lhs,*rhs[2];<br>
> int *p1,*p2;<br>
> double *randoms;<br>
> <br>
> lhs = mxCreateDoubleMatrix(m,no_of_levels,mxREAL); /* m =2, no_of_levels = 4 */<br>
> randoms = mxGetPr(lhs);<br>
> rhs[0] = mxCreateNumericMatrix(1,1,mxUINT8_CLASS,mxREAL);<br>
> rhs[1] = mxCreateNumericMatrix(1,1,mxUINT8_CLASS,mxREAL);<br>
> p1 = mxGetPr(rhs[0]); *p1 = m;<br>
> p2 = mxGetPr(rhs[1]); *p2 = no_of_levels;<br>
> <br>
> mxCallMATLAB(1,lhs,2,rhs,"rand");<br>
> <br>
> but the data is zero in randoms. Where am I going wrong??<br>
> Thanks<br>
<br>
Your basic problem is that mxUINT8_CLASS is a 1byte integer, but your are accessing the data area using an int *, which points to a 4byte integer. This has the potential to bomb MATLAB btw. Try this instead:<br>
<br>
mxArray *lhs,*rhs[2];<br>
double *randoms;<br>
rhs[0] = mxCreateDoubleScalar(m);<br>
rhs[1] = mxCreateDoubleScalar(no_of_levels);<br>
mexCallMATLAB(1,lhs,2,rhs,"rand");<br>
mxDestroyArray(rhs[1]);<br>
mxDestroyArray(rhs[0]);<br>
randoms = mxGetPr(lhs[0]);<br>
// use randoms<br>
mxDestroyArray(lhs[0]);<br>
<br>
James Tursa

Thu, 12 Sep 2013 15:08:06 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911120
James Tursa
"James Tursa" wrote in message <l0sjvm$50f$1@newscl01ah.mathworks.com>...<br>
<br>
> mxArray *lhs,*rhs[2];<br>
<br>
Typo, make the above line:<br>
<br>
mxArray *lhs[1],*rhs[2];<br>
<br>
James Tursa

Fri, 13 Sep 2013 05:06:09 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911155
Hariprasad
"James Tursa" wrote in message <l0slcm$ki8$1@newscl01ah.mathworks.com>...<br>
> "James Tursa" wrote in message <l0sjvm$50f$1@newscl01ah.mathworks.com>...<br>
> <br>
> > mxArray *lhs,*rhs[2];<br>
> <br>
> Typo, make the above line:<br>
> <br>
> mxArray *lhs[1],*rhs[2];<br>
> <br>
> James Tursa<br>
<br>
Hello Sir, I made the changes as directed by you but still the 'randoms' is Zero. I have used the codes as follows: <br>
<br>
double *randoms1, *randoms2; /* need 2 diff sized arrays 2x4 and 2x1 */<br>
mxArray *rhs1[2], *rhs2[2], *lhs1[1], *lhs2[1];<br>
<br>
/* (m=2,no_of_levels=4) from int to double would be a problem?*/ <br>
lhs1[0] = mxCreateDoubleMatrix(m,no_of_levels,mxREAL); <br>
lhs2[0] = mxCreateDoubleMatrix(1,m,mxREAL);<br>
<br>
rhs1[0] = mxCreateDoubleScalar(m);<br>
rhs1[1] = mxCreateDoubleScalar(no_of_levels);<br>
<br>
rhs2[0] = mxCreateDoubleScalar(m); <br>
rhs2[1] = mxCreateDoubleScalar(1);<br>
<br>
mexCallMATLAB(1,lhs1[0],2,rhs1,"rand"); <br>
mxDestroyArray(rhs1[0]);mxDestroyArray(rhs1[1]);<br>
randoms1 = mxGetPr(lhs1[0]);<br>
<br>
like wise I call mexCallMATLAB(1,lhs2[0],2,rhs2,"rand");<br>
<br>
I also use mexEvalString("rand('twister',sum(100*clock));"); <br>
to set the seed before the start of each iteration. Would this be the cause of trouble? Also the randoms1 and randoms2 will be regenerated many times in the code(at least 100 times), so I guess I should use mxDestroyArray at the end of the codes, right?<br>
<br>
Hariprasad

Fri, 13 Sep 2013 05:32:06 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911156
Hariprasad
"Hariprasad" wrote in message <l0u6g1$d3n$1@newscl01ah.mathworks.com>...<br>
> "James Tursa" wrote in message <l0slcm$ki8$1@newscl01ah.mathworks.com>...<br>
> > "James Tursa" wrote in message <l0sjvm$50f$1@newscl01ah.mathworks.com>...<br>
> > <br>
> > > mxArray *lhs,*rhs[2];<br>
> > <br>
> > Typo, make the above line:<br>
> > <br>
> > mxArray *lhs[1],*rhs[2];<br>
> > <br>
> > James Tursa<br>
> <br>
> Hello Sir, I made the changes as directed by you but still the 'randoms' is Zero. I have used the codes as follows: <br>
> <br>
> double *randoms1, *randoms2; /* need 2 diff sized arrays 2x4 and 2x1 */<br>
> mxArray *rhs1[2], *rhs2[2], *lhs1[1], *lhs2[1];<br>
> <br>
> /* (m=2,no_of_levels=4) from int to double would be a problem?*/ <br>
> lhs1[0] = mxCreateDoubleMatrix(m,no_of_levels,mxREAL); <br>
> lhs2[0] = mxCreateDoubleMatrix(1,m,mxREAL);<br>
> <br>
> rhs1[0] = mxCreateDoubleScalar(m);<br>
> rhs1[1] = mxCreateDoubleScalar(no_of_levels);<br>
> <br>
> rhs2[0] = mxCreateDoubleScalar(m); <br>
> rhs2[1] = mxCreateDoubleScalar(1);<br>
> <br>
> mexCallMATLAB(1,lhs1[0],2,rhs1,"rand"); <br>
> mxDestroyArray(rhs1[0]);mxDestroyArray(rhs1[1]);<br>
> randoms1 = mxGetPr(lhs1[0]);<br>
> <br>
> like wise I call mexCallMATLAB(1,lhs2[0],2,rhs2,"rand");<br>
> <br>
> I also use mexEvalString("rand('twister',sum(100*clock));"); <br>
> to set the seed before the start of each iteration. Would this be the cause of trouble? Also the randoms1 and randoms2 will be regenerated many times in the code(at least 100 times), so I guess I should use mxDestroyArray at the end of the codes, right?<br>
<br>
Sorry Sir, I'm getting the results now. A couple of times I tried after compiling my code, it returned Zeros. Then I compiled a couple of times and it gave nonzeros.<br>
Now I just want to make sure I can reuse randoms1 and randoms2 in iterations and destroy arrays at the end of the code OR should I reallocate memory every time I use them and destroy arrays every time as well?<br>
Thanks You<br>
> Hariprasad

Fri, 13 Sep 2013 06:04:06 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911157
Hariprasad
"Hariprasad" wrote in message <l0u80m$8qo$1@newscl01ah.mathworks.com>...<br>
> "Hariprasad" wrote in message <l0u6g1$d3n$1@newscl01ah.mathworks.com>...<br>
> > "James Tursa" wrote in message <l0slcm$ki8$1@newscl01ah.mathworks.com>...<br>
> > > "James Tursa" wrote in message <l0sjvm$50f$1@newscl01ah.mathworks.com>...<br>
> > > <br>
> > > > mxArray *lhs,*rhs[2];<br>
> > > <br>
> > > Typo, make the above line:<br>
> > > <br>
> > > mxArray *lhs[1],*rhs[2];<br>
> > > <br>
> > > James Tursa<br>
> > <br>
> > Hello Sir, I made the changes as directed by you but still the 'randoms' is Zero. I have used the codes as follows: <br>
> > <br>
> > double *randoms1, *randoms2; /* need 2 diff sized arrays 2x4 and 2x1 */<br>
> > mxArray *rhs1[2], *rhs2[2], *lhs1[1], *lhs2[1];<br>
> > <br>
> > /* (m=2,no_of_levels=4) from int to double would be a problem?*/ <br>
> > lhs1[0] = mxCreateDoubleMatrix(m,no_of_levels,mxREAL); <br>
> > lhs2[0] = mxCreateDoubleMatrix(1,m,mxREAL);<br>
> > <br>
> > rhs1[0] = mxCreateDoubleScalar(m);<br>
> > rhs1[1] = mxCreateDoubleScalar(no_of_levels);<br>
> > <br>
> > rhs2[0] = mxCreateDoubleScalar(m); <br>
> > rhs2[1] = mxCreateDoubleScalar(1);<br>
> > <br>
> > mexCallMATLAB(1,lhs1[0],2,rhs1,"rand"); <br>
> > mxDestroyArray(rhs1[0]);mxDestroyArray(rhs1[1]);<br>
> > randoms1 = mxGetPr(lhs1[0]);<br>
> > <br>
> > like wise I call mexCallMATLAB(1,lhs2[0],2,rhs2,"rand");<br>
> > <br>
> > I also use mexEvalString("rand('twister',sum(100*clock));"); <br>
> > to set the seed before the start of each iteration. Would this be the cause of trouble? Also the randoms1 and randoms2 will be regenerated many times in the code(at least 100 times), so I guess I should use mxDestroyArray at the end of the codes, right?<br>
> <br>
> Sorry Sir, I'm getting the results now. A couple of times I tried after compiling my code, it returned Zeros. Then I compiled a couple of times and it gave nonzeros.<br>
> Now I just want to make sure I can reuse randoms1 and randoms2 in iterations and destroy arrays at the end of the code OR should I reallocate memory every time I use them and destroy arrays every time as well?<br>
> Thank You<br>
> > Hariprasad

Fri, 13 Sep 2013 06:30:07 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911159
Hariprasad
<br>
> > > mexCallMATLAB(1,lhs[0],2,rhs,"rand"); <br>
<br>
Could this have been the point of error.. "lhs[0]" ?<br>
Because in my other loop for randoms2 I had put mexCallMATLAB(1,lhs2[0],2,rhs2,"rand");<br>
I got randoms2 as Zero. Then I changed it to <br>
mexCallMATLAB(1,lhs2,2,rhs2,"rand"); and compiled again, got it fine!!!<br>
I'm not sure but that's how it worked for me.<br>
<br>
Thank You<br>
Hariprasad

Fri, 13 Sep 2013 18:50:07 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911209
James Tursa
"Hariprasad" wrote in message <l0ubdf$6o4$1@newscl01ah.mathworks.com>...<br>
> <br>
> > > > mexCallMATLAB(1,lhs[0],2,rhs,"rand"); <br>
> <br>
> Could this have been the point of error.. "lhs[0]" ?<br>
> Because in my other loop for randoms2 I had put mexCallMATLAB(1,lhs2[0],2,rhs2,"rand");<br>
> I got randoms2 as Zero. Then I changed it to <br>
> mexCallMATLAB(1,lhs2,2,rhs2,"rand"); and compiled again, got it fine!!!<br>
> I'm not sure but that's how it worked for me.<br>
<br>
The signature for mexCallMATLAB listed in the documentation is as follows:<br>
<br>
int mexCallMATLAB(int nlhs, mxArray *plhs[], int nrhs,<br>
mxArray *prhs[], const char *functionName);<br>
<br>
The expected type for the 2nd argument is thus mxArray **. But the type of lhs[0] is just mxArray *. That difference, in and of itself, should tell you that the use of lhs[0] for this argument is wrong. That is why you needed to drop the [0] for this argument to work properly.<br>
<br>
James Tursa

Fri, 13 Sep 2013 18:55:06 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911210
James Tursa
"Hariprasad" wrote in message <l0u80m$8qo$1@newscl01ah.mathworks.com>...<br>
> <br>
> Sorry Sir, I'm getting the results now. A couple of times I tried after compiling my code, it returned Zeros. Then I compiled a couple of times and it gave nonzeros.<br>
> Now I just want to make sure I can reuse randoms1 and randoms2 in iterations and destroy arrays at the end of the code OR should I reallocate memory every time I use them and destroy arrays every time as well?<br>
> Thanks You<br>
> > Hariprasad<br>
<br>
If you are repeatedly generating new random number arrays within a C loop, then yes you should use mxDestroyArray on the results each time at the end of each iteration in order to avoid running out of memory since mexCallMATLAB will allocate new memory for the lhs return each time it is called. I'm not sure you got this point earlier so I will state it explicitly now ... you do NOT preallocate the lhs arrays prior to calling mexCallMATLAB. The mexCallMATLAB call itself will allocate whatever it needs for the return value(s) automatically.<br>
<br>
James Tursa

Thu, 19 Sep 2013 06:54:07 +0000
Re: rand() using mexCallMATLAB
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331554#911459
Hariprasad
"James Tursa" wrote in message <l0vn2a$4ob$1@newscl01ah.mathworks.com>...<br>
> "Hariprasad" wrote in message <l0u80m$8qo$1@newscl01ah.mathworks.com>...<br>
> > <br>
> > Sorry Sir, I'm getting the results now. A couple of times I tried after compiling my code, it returned Zeros. Then I compiled a couple of times and it gave nonzeros.<br>
> > Now I just want to make sure I can reuse randoms1 and randoms2 in iterations and destroy arrays at the end of the code OR should I reallocate memory every time I use them and destroy arrays every time as well?<br>
> > Thanks You<br>
> > > Hariprasad<br>
> <br>
> If you are repeatedly generating new random number arrays within a C loop, then yes you should use mxDestroyArray on the results each time at the end of each iteration in order to avoid running out of memory since mexCallMATLAB will allocate new memory for the lhs return each time it is called. I'm not sure you got this point earlier so I will state it explicitly now ... you do NOT preallocate the lhs arrays prior to calling mexCallMATLAB. The mexCallMATLAB call itself will allocate whatever it needs for the return value(s) automatically.<br>
> <br>
> James Tursa<br>
<br>
Thank You Sir. Really helpful insights and solutions. I was able to solve this issue with rand(). This is what I have done: <br>
<br>
mxArray *lhs[1], *rhs[2];<br>
double *randoms;<br>
<br>
rhs[0] = mxCreateDoubleScalar(m); /* m = 2 */<br>
rhs[1] = mxCreateDoubleScalar(no_of_levels); /* no_of_levels = 4 */<br>
<br>
and in the code<br>
mxCallMATLAB(1,lhs,2,rhs,"rand");<br>
randoms = mxGetPr(lhs[0]);<br>
and used randoms for further processing.<br>
<br>
Just need to check when I need to clear and when I should not clear the arrays.<br>
Hariprasad