Compiler / MEX using C help

1 回表示 (過去 30 日間)
Leor Greenberger
Leor Greenberger 2013 年 6 月 17 日
Hi all,
I compiled a matlab function using the compiler toolbox into a dll, and I am trying to call it from my C program. For some reason, the program hangs at the mlfSimpsum(...) line below. Anyone see a mistake I made? The matlab function is very simple:
function c = simpsum(a,b)
c = a + b;
end
My C code:
#include <stdio.h>
#include "main.h"
#include "SimpleSumDemo.h"
int main(void)
{
mxArray *a_ptr, *b_ptr;
mxArray *c_ptr=NULL;
double *c;
a_ptr = mxCreateDoubleScalar(2);
b_ptr = mxCreateDoubleScalar(7);
/* Call the MCR and library initialization functions */
if( !mclInitializeApplication(NULL,0) )
{
fprintf(stderr, "Could not initialize the application.\n");
exit(1);
}
if (!SimpleSumDemoInitialize())
{
fprintf(stderr,"Could not initialize the library.\n");
exit(1);
}
// Call the implementation function
mlfSimpsum(1,&c_ptr,a_ptr,b_ptr);
c = mxGetPr(c_ptr);
printf("2 + 7 = %d\n",&c);
// Call the library termination function
SimpleSumDemoTerminate();
// Clean up references (very important to prevent memory leaks!)
mxDestroyArray(a_ptr);
mxDestroyArray(b_ptr);
mxDestroyArray(c_ptr);
// Call the MCR termination function
mclTerminateApplication();
return 0;
}
  2 件のコメント
Leor Greenberger
Leor Greenberger 2013 年 6 月 17 日
I moved
a_ptr = mxCreateDoubleScalar(2);
b_ptr = mxCreateDoubleScalar(7);
to after the mclInitializeApplication and it now works. However, I get an output now of "2 + 7 = 1638144"
What am I doing wrong?
Leor Greenberger
Leor Greenberger 2013 年 6 月 17 日
編集済み: Leor Greenberger 2013 年 6 月 17 日
Fixed! Although, I wonder if its possible to return a type int and not double?

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by