Mex builds in Matlab, but crashes. Breakpoint not reached in Visual Studio debugging
Mostra commenti meno recenti
Hello,
I've been trying to build a mex file to get some fast C++ computational routine running instead of matlab loops.
The mex builds OK, but matlab crashes when I try to use it in the M console. I just can't figure out how to debug it, because breakpoints are never reached wherever I put them in the cpp in Visual Studio. (Just to precise: I did the debugging with another version automatically provided by matlab coder, but it was slower than the original .m, that's why I tried to write it down by myself)
I'm a beginner with this kind of stuff, and tried to write the code following matlab help and examples given in matlab/extern. This code is about creating a 3D matrix output with computation that uses 3 * 3vectors (3 different sizes, corresponding to the 3 dimensions, and the 3 loops) as input.
Here it is, it is rather short. If someone can look at it and tell me if something looks wrong, any help would be greatly appreciated.
#include <cmath>
#include "mex.h"
/* The computational routine */
void comp_R( double *Xobs, double *Yobs, double *Zobs,
double *x, double *y, double *z,
double *X, double *Y, double *Z, double *R,
mwSize M, mwSize T, mwSize GS)
{
mwSize m,t,gs;
for(m=0; m < M ; m++)
{
for(t=0; t < T ; t++)
{
for(gs=0; gs < GS ; gs++)
{
R[gs*(T*M)+t*M+m]=sqrt((Xobs[gs]+(x[t]-x[0])-X[m])*(Xobs[gs]+(x[t]-x[0])-X[m])+(Yobs[gs]+(y[t]-y[0])-Y[m])*(Yobs[gs]+(y[t]-y[0])-Y[m])+(Zobs[gs]+(z[t]-z[0])-Z[m])*(Zobs[gs]+(z[t]-z[0])-Z[m]));
/*delay[gs][t][m]=R[gs][t][m]/c;*/
}
}
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* check number of arg */
if(nrhs!=9) {
mexErrMsgIdAndTxt("MyToolbox:vec_geom:nrhs","Nine inputs required.");
}
if(nlhs!=1) {
mexErrMsgIdAndTxt("MyToolbox:vec_geom:nlhs","One output required.");
}
/* variables initialisation*/
double *xMatrix,*yMatrix,*zMatrix,*XMatrix,*YMatrix,*ZMatrix,*XobsMatrix,*YobsMatrix,*ZobsMatrix; // input matrices
double *outMatrix; // output matrix
mwSize n_GS,n_T,n_M; // grid size, time samples, number of microphones
mwSignedIndex dims[3];
/* create a pointer to the real data in the input matrix and get dimensions of the input matrix */
XobsMatrix = mxGetPr(prhs[0]); n_GS = mxGetN(prhs[0]);
YobsMatrix = mxGetPr(prhs[1]);
ZobsMatrix = mxGetPr(prhs[2]);
xMatrix = mxGetPr(prhs[3]); n_T = mxGetN(prhs[3]);
yMatrix = mxGetPr(prhs[4]);
zMatrix = mxGetPr(prhs[5]);
XMatrix = mxGetPr(prhs[6]); n_M = mxGetN(prhs[6]);
YMatrix = mxGetPr(prhs[7]);
ZMatrix = mxGetPr(prhs[8]);
/* create output matrix */
dims[0] = n_GS ;
dims[1] = n_T ;
dims[2] = n_M ;
plhs[0] = mxCreateNumericArray(3, dims, mxSINGLE_CLASS, mxREAL);
/* get a pointer to the real data in the output matrix */
outMatrix = mxGetPr(plhs[0]);
/* call the computational routine */
comp_R(XobsMatrix,YobsMatrix,ZobsMatrix,xMatrix,yMatrix,zMatrix,XMatrix,YMatrix,ZMatrix,outMatrix,n_GS,n_T,n_M);
}
I can give the error log if needed (stack trace that comes after "Abnormal termination: Segmentation violation" is just meaningless to me...)
Thanks, Raphaël
Risposta accettata
Più risposte (1)
John Doe
il 13 Giu 2013
0 voti
Categorie
Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!