I have a problem about mex file!
이전 댓글 표시
I wrote the following file.c:
void mexFunction(int nlhs, mxArray * plhs [], int nrhs, const mxArray * prhs []) { double * value; const mwSize * dimx; int nrows, ncolums; int i, j; int lunghy; int valz;
char * stringaval;
value = mxGetPr(prhs[0]);
dimx = mxGetDimensions(prhs[0]);
nrows = (int)dimx [0];
ncolums = (int) dimx [1];
for (i=0; i<nrows;i++)
{
for(j=0; j<ncolums; j++)
{
// stampo il valore float e vado a stampare la prima riga
printf("%f",value [j*nrows+1]);
}
printf("%n");
}
lunghy = (int) mxGetN(prhs[1])+1;
stringaval = mxCalloc(lunghy, sizeof(char));
mxGetString(prhs[1],stringaval,lunghy);
printf("%s",stringaval);
valz = (int) mxGetScalar(prhs[2]);
printf("%d\n",valz);
}
But, when I go to compile it on matlab >>mex mynamefile.c >>mynamefile([1 3 4;-1 -2 -4],'bye',10) matlab closes! I can't understand if there is an error in the code or is my matlab doesn't work. The version I am using is the Matalb R2013b Help me... please!
댓글 수: 4
Geoff Hayes
2014년 8월 31일
Lucy - I was able to compile your program (without errors) once I put the correct angled brackets and double quotes around your include files
#include <math.h>
#include "matrix.h"
#include "mex.h"
Replace your three lines with the above and try compiling again.
Lucy
2014년 8월 31일
Geoff Hayes
2014년 8월 31일
Have you ever built any other MEX-functions? Have you run mex -setup?
Lucy
2014년 8월 31일
답변 (1개)
Geoff Hayes
2014년 8월 31일
Lucy - I was able to reproduce the crash; MATLAB does not indeed close when you run the compiled program. The problem is with this line
printf("%n");
I realize that you probably want a carriage return here, after printing out each row of the matrix. So please replace the above line with
printf("\n");
The other line shouldn't cause a crash; even creating the function as a cpp file and including a try/catch block does not prevent this behaviour.
With the above fix, running your program produces
>> g1([1 3 4;-1 -2 -4],'bye',10)
-1.000000-2.000000-4.000000
-1.000000-2.000000-4.000000
bye10
Try the above and see what happens!
카테고리
도움말 센터 및 File Exchange에서 Write 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!
