MATLAB crashes when I run MEX file - HELP

10 次查看(过去 30 天)
Hello. I am trying to write a mex file through MATLAB. This is my first time to deal with C code in general although I have a coding experience in Java. After some research and the test code that I wrote in MATLAB, I successfully implemented mex file and ran fine. After that I tried to add some more code in existing code, MATLAB crashes up every time I ran it. I know that there is not many problems, but I quite don't know how to fix this error.
Here is a code that I wrote. I know that this code is very clumsy and it's because it's my first time to code c. The issue that I have with is almost the last line of the code; for loop.
#include stdio.h
#include stdlib.h
#include math.h
#include "mex.h"
#include "matrix.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
double *temp, *proj, *proj_spt, *feature, *stim, *spt;
int i, ii, Nstim, Mstim, Nfeat, Mfeat, Mspt, Nspt;
Mstim = mxGetM(prhs[0]); /* Get a dimension of stim */
Nstim = mxGetN(prhs[0]);
Mspt = mxGetM(prhs[2]); /* Get a dimension of spt */
Nspt = mxGetN(prhs[2]);
Mfeat = mxGetM(prhs[1]);
Nfeat = mxGetN(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(Mstim, Nstim, mxREAL);
proj = mxGetPr(plhs[0]);
plhs[1] = mxCreateDoubleMatrix(Mstim, Nstim, mxREAL);
proj_spt = mxGetPr(plhs[1]);
feature = mxGetPr(prhs[1]);
stim = mxGetPr(prhs[0]);
spt = mxGetPr(prhs[2]);
for (i=0;i<(Nstim-Nfeat+1);i++){
temp[i] = 0;
for (ii = 0; ii < Nfeat; ii++){
temp[i] = temp[i] + (feature[ii]*stim[i+ii]);
}
}
for (i=(Nstim-Nfeat+1);i<Nstim;i++){
temp[i] = 0;
for (ii = 0; ii < (Nstim-i); ii++){
temp[i] = temp[i] + (feature[ii]*stim[i+ii]);
}
}
for (i=0;i<Nstim;i++){
proj[i] = temp[i];
}
// This part has an error.
for (i=0;i<Nspt;i++){
ii = spt[i];
mexPrintf("%d is spike time. \n",ii);
}
As you can see, I have tried somethings and really don't want to try more because every time I run this, MATLAB crashes, which is very annoying. The fact that I don't know how to debug the mex file also is being a reason for me not to try.
Could anyone help me out to get through this error? Also I notice that it is not possible for the one to use Mstim (in this case) as an index of the vector or array..Why is it not possible?
For example, I noticed that I get an error if I do this.
for (i = Nfeat ; i < Nstim , i++){
proj[i] = temp[i-Nfeat];
}
Why does this cast an error? FYI, Nstim = 15 and Nfeat = 4 , which is supposed to be length(stim) = 15 and length(feat) = 4 in MATLAB.

回答(1 个)

James Tursa
James Tursa 2014-3-7
I don't see any place in your code where you allocate temp. So the first time you use it you dereference an invalid pointer, and MATLAB crashes. Try adding this before using temp:
temp = mxMalloc(Nstim * sizeof(*temp));
And then at the end of your code:
mxFree(temp);
  1 个评论
James Tursa
James Tursa 2014-3-7
P.S. You don't need to explicitly #include "matrix.h" since that comes along with mex.h

请先登录,再进行评论。

类别

Help CenterFile 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!

Translated by