From: James Tursa on
"Kimonas Fountoulakis" <kfoynt(a)hotmail.com> wrote in message <hujp58$dv1$1(a)fred.mathworks.com>...
> It worked!!!!!!! THANKS!!
>
> pls, coud you explain me why this doesn't work??
>
> mxArray *lhs[5];
> mxArray *rhs;
>
> rhs = mxCreateDoubleMatrix(6, 1, mxREAL);
> double *x = mxGetPr(rhs);
>
> x[0] = 2; x[1] = 2; x[2] = 2; x[3] = 0; x[4] = 0; x[5] = 0;
>
> mexCallMATLAB(5, lhs, 6, rhs, "getData");
>
> Is there a problem if I declare rhs as a matrix pass it into the x pointer, and pass the values into the memory through x??? and if yes, why??

First, you are not passing the correct type for the 4th argument. The 2nd and 4th arguments need to be an array of mxArray*, or an mxArray**, but what you have passed in the 4th spot is an mxArray*. So the dereferencing is at a different level of indirection and you will get a crash as a result. Basically, the lhs and rhs should be arrays of mxArray* values (or the equivalent).

Second, even if you fixed up the 4th argument to be &rhs to get the correct level of indirection, you have lied to mexCallMATLAB and told it there are 6 arguments present in rhs when in fact there is only one. i.e., rhs is a single mxArray* so &rhs points to a place in memory that contains only one mxArray*, not 6 of them. In essence, what you have done with the above code is (almost) the equivalent of doing this at the MATLAB command line:

[lhs0 lhs1 lhs2 lhs3 lhs4] = getData([2;2;2;0;0;0]) %except you lied to mexCallMATLAB and told it there were 6 inputs.

i.e., you passed it a single 6x1 variable instead of doing this:

[lhs0 lhs1 lhs2 lhs3 lhs4] = getData(2,2,2,0,0,0)

which is passing 6 separate variables (and is what my corrected code does).

James Tursa
First  |  Prev  | 
Pages: 1 2
Prev: randomly moving spheres
Next: hmm, initial matrix