[mexFunction in Matlab] printf() function in C code

조회 수: 8 (최근 30일)
shdotcom shdotcom
shdotcom shdotcom 2019년 9월 23일
답변: James Tursa 2019년 9월 23일
If I have a C code that consists of three files, Afun.c, Bfun.c and Cfun.c
int Afun (int argc, char *argv[]){)
{
Bfun();
// ....
int aa = 5;
return aa;
}
void Bfun (void)
{
Cfun();
}
void Cfun (void)
{
printf ( "\n This is C function\n");
}
In the mexFunction in Matlab, how can I show the output of printf() of Cfun()? where Afun() is the main function in C code.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int argc = 0;
char **argv;
int i; result
argc = nrhs;
argv = (char **) mxCalloc( argc, sizeof(char *) );
for (i = 0; i < nrhs; i++){
argv[i] = mxArrayToString( prhs[i] );
}
// .....
result = Afun(argc, argv);
plhs[0] = mxCreateDoubleScalar(result);
}

답변 (1개)

James Tursa
James Tursa 2019년 9월 23일
How you have it written should work, except maybe replace printf( ) with mexPrintf( ). Also, depending on your compiler and settings, you may need to move the Bfun( ) call after the int aa definition. Are you having problems with this not displaying anything?

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by