Info

This question is closed. Reopen it to edit or answer.

Mex-File issue : correlated gaussians

1 view (last 30 days)
Brian
Brian on 17 Aug 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello, I wrote the following code in a MexFunction:
void Gaussians(double *H, int N, double *W)
{
/* computes the matrix multiplication H*R where H is a (N x N) matrix
* and R is a (N x 1) gaussian vector */
mxArray *rhs1[2], *rhs2[2], *lhs1[1], *lhs2[1];
rhs1[0] = mxCreateDoubleScalar(N);
rhs1[1] = mxCreateDoubleScalar(1);
/* generates R = randn(N, 1) */
mexCallMATLAB(1, lhs1, 2, rhs1, "randn");
rhs2[0] = mxGetPr(H);
rhs2[1] = lhs1[0];
W = mxGetPr(lhs2[0]);
/* computes H*R */
mexCallMATLAB(1, lhs2, 2, rhs2, "mtimes");
}
There is no problem with the mex compilation but when I run the program, I get an "Acces violation" error and matlab crashes. I was not able to find where the problem comes from.
Thank you for your help
PS : I'm starting with C language and Mex-Files.

Answers (1)

Jan
Jan on 11 Mar 2016
rhs2[0] = mxGetPr(H);
On the left side you have pointer to an mxArray, on the right you try to get the pointer to the double data of an myArray, but the argument is a pointer to a double already. It is surprising that the compiler accepts this. The next line contains similar problems:
rhs2[1] = lhs1[0];
What do you want to achieve?

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!