Problem with mex file!
Mostrar comentarios más antiguos
Hello there, I'm writing some codes in matlab that calls some mex subroutines. I don't know why, I have a problem with one of these mex subroutines: MATLAB is working well when I call it with 7 inputs, but it is crashing, as I add an eighth variable as imput.
This is the main code:
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
#include<time.h>
#include <unistd.h>
#include <mex.h>
#include <matrix.h>
void rejectsam(double *y, double *p, double *r, double *w, double *ww, double *mx, double *cv, double *c2v, double *u, double *xx) {
int j,t,h, flag, count, m, m2;
double meanz[2][1]={},sigmz[2][2]={}, meanz2[6][1]={},sigmz2[6][6]={}, Ycat[76672]={}, Ycat2[76672]={}, aux2[2][1]={}, aux[6][1]={}, aux3[3][1]={},aux3b[3][1]={}, L2[2][2]={}, L6[6][6]={},Z[8][76672]={}, omegai[9][9]={},
beta[9]={}, Ycont[76672]={},meanb[2][1]={}, sigmab[2][2]={}, meany12, vary12, mis[76672]={}, invaux[3][3]={}, invomega[3][3]={}, omega0238[6][3]={}, omega0238t[3][6]={}, interm[6][3]={}, interm2[6][6]={}, cov[76672], cov2[76672];
memcpy(&omegai, y, sizeof(omegai));
memcpy(&beta, p, sizeof(beta));
memcpy(&Ycont, r, sizeof(Ycont));
memcpy(&Ycat, w, sizeof(Ycat));
memcpy(&Ycat2, ww, sizeof(Ycat2));
memcpy(&mis, mx, sizeof(mis));
memcpy(&cov, cv, sizeof(cov));
memcpy(&cov2, c2v, sizeof(cov2));
for (j=0;j<3;j++) {
for (t=0;t<3;t++) invaux[t][j]=omegai[t][j];
}
for (j=0;j<2;j++) {
sigmab[j][j]=1;
meanb[j][0]=beta[j+1];
}
/*here I don't copy some stuff that doesn't matter because it still crash if I put them as comment*/
memcpy(u, &Z, sizeof(Z));
memcpy(xx, &Ycont, sizeof(Ycont));
printf("%f %f %f %f\n",omegai[0][0], cov[0], beta[2], cov2[20000]);
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *y,*u, *p, *w, *r, *mx, *xx, *ww, *cv, *c2v;
/* check for proper number of arguments */
/* NOTE: You do not need an else statement when using mexErrMsgIdAndTxt
within an if statement, because it will never get to the else
statement if mexErrMsgIdAndTxt is executed. (mexErrMsgIdAndTxt breaks you out of
the MEX-file) */
if(nrhs!=8)
mexErrMsgIdAndTxt( "MATLAB:xtimesy:invalidNumInputs",
"Eight inputs required.");
if(nlhs!=2)
mexErrMsgIdAndTxt( "MATLAB:xtimesy:invalidNumOutputs",
"Two output required.");
/* create a pointer to the input matrix y */
y = mxGetPr(prhs[0]);
/* create a pointer to the input matrix p*/
p = mxGetPr(prhs[1]);
/* create a pointer to the input matrix r */
r = mxGetPr(prhs[2]);
/* create a pointer to the input matrix w */
w = mxGetPr(prhs[3]);
/* create a pointer to the input matrix ww */
ww = mxGetPr(prhs[4]);
/* create a pointer to the input matrix mx */
mx = mxGetPr(prhs[5]);
/* create a pointer to the input matrix cv */
cv = mxGetPr(prhs[6]);
/* create a pointer to the input matrix c2v */
c2v = mxGetPr(prhs[7]);
/* set the output pointer to the output matrix */
plhs[0] = mxCreateDoubleMatrix( 8, 76672, mxREAL);
/* create a C pointer to a copy of the output matrix */
u = mxGetPr(plhs[0]);
/* set the output pointer to the output matrix */
plhs[1] = mxCreateDoubleMatrix( 1, 76672, mxREAL);
/* create a C pointer to a copy of the output matrix */
xx = mxGetPr(plhs[1]);
/* call the C subroutine */
rejectsam(y,p,r,w,ww,mx,cv,c2v,u,xx);
}
It all works perfectly until I add the variable c2v, which I'm sure is a 76672 doubles vector. It is 3 days that I'm trying to sort it out, the only thing I can imagine is that maybe there is some limit in the number of inputs I can pass to a mex function... In the last line of the main program, if I didn't put between the variables to print cov2, then the program works, so it seems that it crashes only when I consider this variable... Do you have any idea? Thanks in advance! Ps: when I say "crashes" I mean that Matlab window just closes without any message or so, I just have to re-launch Matlab...
2 comentarios
There is no magic limit for the number of inputs.
Could you please specify where the function crashes?
Your methods to copy the values could be simplified, when you use the count j for bother sides, or even better, call memcpy instead of the loops.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!