Mex file crash when called second time

I am a new hand at mex. I just finished a test mex file of matrix addition. But it crashed when i called it the second time. Then I tried restarting several times, but it still couldn't work.
A little crazy...Really look forward to your solution.
PS:version : 2013a compiler: VS2012 system: win7 Ultimate 64bit
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
mxArray *a;
mxArray *b;
mxArray *out;
mwSize m,n;
int i;
int j;
a = plhs[0];
b = prhs[1];
m = mxGetM(plhs[0]);
n = mxGetM(prhs[1]);
if(m!=n)
{
mexErrMsgTxt("Dimensions mismatch!");
}
else
{
m = mxGetN(plhs[0]);
n = mxGetN(prhs[1]);
if(m!=n)
mexErrMsgTxt("Dimensions mismatch!");
else
m = mxGetM(plhs[0]);
n = mxGetN(plhs[0]);
}
for(i=0; i< m ; i++)
for(j =0; j<n; j++)
{
*(mxGetPr(out) + i *n +j) = *(mxGetPr(a) + i *n +j)+*(mxGetPr(b) + i *n +j);
}
mexPrintf("addition is done");
}

답변 (1개)

James Tursa
James Tursa 2013년 5월 21일
편집: James Tursa 2013년 5월 21일

1 개 추천

I have taken your code and made several corrections. By comparing side by side you should be able to tell what the errors were. Pay special attention to what Friedrich has already written ... you need to create the output plhs[0] yourself, MATLAB does not do this for you. I reordered your loops so that the inner loop goes over the row indexes first ... that way the elements are accessed in order as they appear in memory, which is better for high level caching. I didn't do it obviously, but you could rewrite your loops as a single loop using mxGetNumberOfElements to handle any dimension of inputs, not just 2D. That will be left as an exercise for the reader ...
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *a;
mxArray *b;
mxArray *out;
mwSize m, n;
int i;
int j;
double *apr, *bpr, *outpr;
if( nrhs != 2 || !mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]) ||
mxIsSparse(prhs[0]) || mxIsSparse(prhs[1]) ||
mxIsComplex(prhs[0]) || mxIsComplex(prhs[1]) ||
mxGetNumberOfDimensions(prhs[0]) != 2 ||
mxGetNumberOfDimensions(prhs[1]) != 2 ) {
mexErrMsgTxt("Need two full real double 2D matrix inputs.");
}
if( nlhs > 1 ) {
mexErrMsgTxt("Too many outputs.");
}
a = prhs[0];
b = prhs[1];
m = mxGetM(a);
n = mxGetM(b);
if( m != n ) {
mexErrMsgTxt("Dimensions mismatch!");
} else {
m = mxGetN(a);
n = mxGetN(b);
if( m != n ) {
mexErrMsgTxt("Dimensions mismatch!");
} else {
m = mxGetM(a);
n = mxGetN(a);
plhs[0] = mxCreateDoubleMatrix( m, n, mxREAL );
out = plhs[0];
apr = mxGetPr(a);
bpr = mxGetPr(b);
outpr = mxGetPr(out);
for( j =0; j<n; j++ ) {
for( i=0; i< m ; i++ ) {
*(outpr + j*m +i) = *(apr + j*m + i) + *(bpr + j*m + i);
// *outpr++ = *apr++ + *bpr++; // could have done this instead
}
}
}
}
mexPrintf("addition is done\n");
}

카테고리

도움말 센터File Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

제품

태그

질문:

2013년 5월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by