MATLAB crashes when running mex file with recursive function
Afficher commentaires plus anciens
I created a mex file and compiled it successfully but MATLAB crashes when I run the file. Below is the mex file I created. It uses a recursive function.
# include "mex.h"
double B(mwSize l,double *t_arr,mwSize m,mwSize p,double *t_array){
double b,w1,w2;
if (t_arr[l]>=t_array[m] && t_arr[l]<=t_array[m+1] && p==1)
{ b=1;
}
else if (p>1){
if ((t_array[m+p-1]-t_array[m])==0)
{
w1=0;
}
else{
w1=(t_arr[l]-t_array[m])/(t_array[m+p-1]-t_array[m]);
}
if ((t_array[m+p-1]-t_array[m+1])==0)
{
w2=0;
}
else{
w2=(t_arr[l]-t_array[m+1])/(t_array[m+p-1]-t_array[m+1]);
}
b=(w1*B(l,t_arr,m,p-1,t_array))+((1-w2)*B(l,t_arr,m+1,p-1,t_array));
}
else
{ b=0;
}
return b;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Check number of inputs:
if (nrhs != 5) {
mexErrMsgIdAndTxt("JSimon:Func:BadNInput",
"5 inputs required.");
}
if( !mxIsDouble(prhs[0]) ||
mxIsComplex(prhs[0]) ||
mxGetNumberOfElements(prhs[0]) != 1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notScalar",
"Input multiplier must be a scalar.");
}
/* check that number of rows in second input argument is 1 */
if(mxGetM(prhs[1]) != 1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector",
"Input must be a row vector.");
}
if( !mxIsDouble(prhs[2]) ||
mxIsComplex(prhs[2]) ||
mxGetNumberOfElements(prhs[2]) != 1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notScalar",
"Input multiplier must be a scalar.");
}
if( !mxIsDouble(prhs[3]) ||
mxIsComplex(prhs[3]) ||
mxGetNumberOfElements(prhs[3]) != 1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notScalar",
"Input multiplier must be a scalar.");
}
/* check that number of rows in second input argument is 1 */
if(mxGetM(prhs[4]) != 1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector",
"Input must be a row vector.");
}
double *t_arr, *t_array; // inputs
double b; // output
mwSize l, m, p;
// Read inputs:
l = (mwSize) mxGetScalar(prhs[0]);
m = (mwSize) mxGetScalar(prhs[3]);
p = (mwSize) mxGetScalar(prhs[4]);
t_arr = mxGetPr(prhs[2]);
t_array = mxGetPr(prhs[5]);
// Create output:
plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL);
b = (double) mxGetScalar(plhs[0]);
// The computations:
b=B(l,t_arr,m,p,t_array);
return;
}
4 commentaires
Jan
le 2 Nov 2017
This is not the cause of the crash, but a mistake:
b = (double) mxGetScalar(plhs[0]);
You create a variable and reads its contents into the variable b. Later on you overwrite b, but this will not be forwarded to the caller. You need:
double *bp;
plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL);
bp = mxGetPr(plhs[0]);
*bp = B(l,t_arr,m,p,t_array);
Deepak Sridhar
le 2 Nov 2017
James Tursa
le 2 Nov 2017
How are you calling this from your m-code? What are the argument types and sizes?
Deepak Sridhar
le 2 Nov 2017
Modifié(e) : Deepak Sridhar
le 2 Nov 2017
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!