MATLAB has encountered an internal problem and needs to close

It always happens when I run a certain mex file on certain data. I've tested the mex file on other data and it works fine. It's a huge amount of data. What is the problem? Thanks, Alex

1 comentario

Without more information, all we can venture are wild guesses.
It sounds like you are running into memory problems. It is possible that the system cannot allocate enough memory. Depending on how that is handled in the mex-code, it is possible that that generates your error.

Iniciar sesión para comentar.

Respuestas (3)

James Tursa
James Tursa el 23 de Jun. de 2014
Editada: James Tursa el 23 de Jun. de 2014
This usually indicates a coding error in the mex routine itself. E.g., not checking for valid number of inputs or class of input variables before using them, not checking for size of input variables, etc. You can solve the issue usually by updating the source code to be more robust and then recompiling the mex routine. We would need to see the source code before we can offer any specific advice. Do you have the source code for the mex routine? Did you write this mex routine?

1 comentario

James Tursa
James Tursa el 23 de Jun. de 2014
Editada: James Tursa el 23 de Jun. de 2014
Looking at the code you have posted, the inputs are assumed to be double as you surmised and will not work (and likely crash) for other variable types. Your options appear to be either (1) Add code for other input variable types (e.g., duplicate pretty much the entire code for each variable type you want the routine to handle), or (2) Cast your inputs to double before calling the routine. Since (1) is a lot of work, I would suggest (2) if at all possible.

Iniciar sesión para comentar.

Alexander
Alexander el 23 de Jun. de 2014
Editada: Alexander el 23 de Jun. de 2014
Here it is. mexFunction is the driver, below dshift2. It doesn't work if the inputs for U and V are ints, they have to be doubles. How can I make my code more robust? You'll have to copy it into a sane text editor, the forum screws up the code. Thanks again, Alex
#include "mex.h"
#include <math.h>
double dshift2(double* u, double* v, int max_shift, int TT)
{
printf("hia\n");
double minner = 0.0;
for(int t = 0; t < TT; t++)
{
minner += pow(u[t] - v[t],2.0);
}
printf("hib\n");
minner = minner/((double)(TT));
for(int s = 1; s <= max_shift; s++)
{
double minmod = 0.0;
for(int t = 0; t < TT-s; t++)
{
minmod = minmod + pow(u[t]-v[t+s],2.0);
}
printf("hic\n");
minmod = minmod/((double)(TT-s));
if(minmod < minner)
{
minner = minmod;
}
minmod = 0.0;
printf("hid\n");
for(int t = 0; t < TT-s; t++)
{
minmod = minmod + pow(u[t+s]-v[t],2.0);
}
minmod = minmod/((double)(TT-s));
printf("hie\n");
if(minmod < minner)
{
minner = minmod;
}
printf("hif\n");
}
return minner;
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// U and V must not be integers!
#define Up prhs[0]
#define Vp prhs[1]
#define rbf_sigmap prhs[2]
#define max_shiftp prhs[3]
#define TTp prhs[4]
#define Kp plhs[0]
printf("hi1\n");
double* U = mxGetPr(Up);
double* V = mxGetPr(Vp);
double* rbf_sigma0 = mxGetPr(rbf_sigmap);
double* max_shift0 = mxGetPr(max_shiftp);
double* TT0 = mxGetPr(TTp);
printf("hi2\n");
double rbf_sigma = rbf_sigma0[0];
int max_shift = (int)max_shift0[0];
int TT = (int)TT0[0];
printf("hi3\n");
int sU = mxGetM(Up);
int sV = mxGetM(Vp);
Kp = mxCreateDoubleMatrix(sU,sV,mxREAL);
double* K = mxGetPr(Kp);
printf("hi4\n");
int N = mxGetN(Up)/TT;
mxArray* up = mxCreateDoubleMatrix(TT,1,mxREAL);
mxArray* vp = mxCreateDoubleMatrix(TT,1,mxREAL);
double* u = mxGetPr(up);
double* v = mxGetPr(vp);
printf("hi5\n");
for(int i = 0; i < sU; i++)
{
for(int j = 0; j < sV; j++)
{
K[i + j*sU] = 0;
for(int k = 0; k < N; k++)
{
for(int t = 0; t < TT; t++)
{
u[t] = U[i + k*sU*TT + t*sU];
v[t] = V[j + k*sV*TT + t*sV];
}
K[i + j*sU] = K[i + j*sU] + dshift2(u,v,max_shift,TT);
printf("hi6\n");
}
K[i + j*sU] = exp(-K[i + j*sU]/(2.0 * pow(rbf_sigma,2.0)));
printf("hi7\n");
}
printf("%d/%d\n",i,sU);
}
return;
}

1 comentario

José-Luis
José-Luis el 23 de Jun. de 2014
Editada: José-Luis el 23 de Jun. de 2014
Please place this as a comment to your question and not as an answer.

Iniciar sesión para comentar.

Alexander
Alexander el 23 de Jun. de 2014
Note: it only seems to break when I call it from svmtrain ...
TT = T-125+1; max_shift = 0;
tic rbf_sigma = 250; bc = 3; svmstruct = svmtrain(Xtrain,ytrain,'kernel_function', ... @(U,V)kshiftmex(U,V,rbf_sigma,max_shift,TT), ... 'boxconstraint',bc, ... 'method','LS', ... 'autoscale',0); group = svmclassify(svmstruct,Xtest); pbase = mean(group == ytest); disp(pbase); toc

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.

Preguntada:

el 23 de Jun. de 2014

Editada:

el 23 de Jun. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by