Calling a DLL generated by MatLab from VC

Matlab version is 2009b, and VC version is 2008,the program is calling a DLL generated by the Matlab function normrnd.m to obtain 100 random numbers.
mxArray *norm_out;
mxArray *norm_in[4];
double r[100];
int main()
{
if ( !mclInitializeApplication(NULL,0) )
{
fprintf(stderr, "Could not initialize the application.\n" );
printf("Hello\n");
exit(1);
}
if (!libpkgInitialize())
{
fprintf(stderr, "Could not initialize the library.\n" );
exit(1);
}
norm_in[0] = mxCreateScalarDouble(0.0);
norm_in[1] = mxCreateScalarDouble(1.0);
norm_in[2] = mxCreateScalarDouble(1);
norm_in[3] = mxCreateScalarDouble(100);
norm_out = mxCreateDoubleMatrix(1,100,mxREAL);
mlxNormrnd(1,norm_out,4,norm_in);
memcpy(r,norm_out,100*sizeof(double))
However, the outputs of array r are all zeros. What's the problem?

답변 (1개)

Titus Edelhofer
Titus Edelhofer 2012년 7월 26일
편집: Titus Edelhofer 2012년 7월 26일

0 개 추천

Hi,
it's the last memcpy that's wrong: you should not use the datastructure norm_out, but the data of norm_out, i.e.,
memcpy(r,mxGetPr(norm_out),100*sizeof(double))
Alternative: at the top write
double *r = null;
and instead of a memcpy just do
r = mxGetPr(norm_out);
Titus
EDIT: changed mxGetPr call.

댓글 수: 5

iar
iar 2012년 7월 26일
I'm using C not C++ to call the DLL, I try it but cannot solve the problem, can you tell me more details, and what's the differences between the two methods?
Thanks.
Titus Edelhofer
Titus Edelhofer 2012년 7월 26일
Hi Iar,
my fault: I've been programming too much in object oriented languages the last days ... I changed the call to mxGetPr to be correct C syntax now.
Thanks, Titus
iar
iar 2012년 7월 26일
I think that's no difference between double *r and double r[100],and I modify the code in your way, but the problem is still there, could you practice it and tell me your result?
Thanks.
Hi,
of course, the declaration of r as double* or double r[100] is the same.
Nnow I see another mistake: don't initialize norm_out, i.e., replace the line
norm_out = mxCreateDoubleMatrix(...);
by
norm_out = null;
Titus
iar
iar 2012년 7월 29일
The problem is solved when I modify the function: mlxNormrnd(1,norm_out,4,norm_in); into mlxNormrnd(1,&norm_out,4,norm_in);
Thanks for your attention.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

태그

질문:

iar
2012년 7월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by