mex compile error caused by different position of mexPrintf()
이전 댓글 표시
I have got this code from website, saved it as hello.c and set up mex using MSVC 2010 compiler. It gives me compile errors. Such as illegal use of mxArray and undefined variables error.
#include <matrix.h>
#include <math.h>
#include <mex.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
mexPrintf("hello, world!\n");
//declare variables
mxArray *a_in_m;
mxArray *b_in_m;
mxArray *c_out_m;
mxArray *d_out_m;
const mwSize *dims;
double *a, *b, *c, *d;
int dimx, dimy, numdims;
//associate inputs
a_in_m = mxDuplicateArray(prhs[0]);
b_in_m = mxDuplicateArray(prhs[1]);
//figure out dimensions
dims = mxGetDimensions(prhs[0]);
numdims = mxGetNumberOfDimensions(prhs[0]);
dimy = (int)dims[0]; dimx = (int)dims[1];
//associate outputs
c_out_m = plhs[0] = mxCreateDoubleMatrix(dimy, dimx, mxREAL);
d_out_m = plhs[1] = mxCreateDoubleMatrix(dimy, dimx, mxREAL);
}
However, when I put the mexPrintf sentence in after the declaration part. The errors are gone.
#include <matrix.h>
#include <math.h>
#include <mex.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
//declare variables
mxArray *a_in_m;
mxArray *b_in_m;
mxArray *c_out_m;
mxArray *d_out_m;
const mwSize *dims;
double *a, *b, *c, *d;
int dimx, dimy, numdims;
mexPrintf("hello, world!\n");
//associate inputs
a_in_m = mxDuplicateArray(prhs[0]);
b_in_m = mxDuplicateArray(prhs[1]);
//figure out dimensions
dims = mxGetDimensions(prhs[0]);
numdims = mxGetNumberOfDimensions(prhs[0]);
dimy = (int)dims[0]; dimx = (int)dims[1];
//associate outputs
c_out_m = plhs[0] = mxCreateDoubleMatrix(dimy, dimx, mxREAL);
d_out_m = plhs[1] = mxCreateDoubleMatrix(dimy, dimx, mxREAL);
}
Could anyone explain why this happens? Thanks!
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 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!