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 件のコメント

Jan
Jan 2017 年 11 月 2 日
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
Deepak Sridhar 2017 年 11 月 2 日
Thanks Jan! But MATLAB still crashes when I run the file. Is there any specific way to run the file?
James Tursa
James Tursa 2017 年 11 月 2 日
How are you calling this from your m-code? What are the argument types and sizes?
Deepak Sridhar
Deepak Sridhar 2017 年 11 月 2 日
編集済み: Deepak Sridhar 2017 年 11 月 2 日
I saved the file as B.c. After compiling, I get the file B.mexw64
a=0;
b=10;
p=3;
n=17;
f=n+p;
t_arr=linspace(a,b,f);
t_array=linspace(a,b,1000);
b1=0;
for i = 1:(length(t_array))
bl=bl+(B(i,t_array,1,p,t_arr)*B(i,t_array,1,p,t_arr)); // calling here
end

サインインしてコメントする。

 採用された回答

James Tursa
James Tursa 2017 年 11 月 2 日
編集済み: James Tursa 2017 年 11 月 2 日

0 投票

You've got your C mex indexing wrong. Remember, C is 0-based indexing, not 1-based indexing. So in these lines:
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]);
The available indexes for prhs are 0,1,2,3,4 since you are calling the B mex routine with five inputs. That last line above uses prhs[5] which means you are reading beyond the valid indexing for prhs, so you get a memory access error and a seg fault. What you probably need to use is this:
l = (mwSize) mxGetScalar(prhs[0]);
m = (mwSize) mxGetScalar(prhs[2]);
p = (mwSize) mxGetScalar(prhs[3]);
t_arr = mxGetPr(prhs[1]);
t_array = mxGetPr(prhs[4]);

3 件のコメント

Deepak Sridhar
Deepak Sridhar 2017 年 11 月 2 日
Thanks James! But why doesn't it show an error?
James Tursa
James Tursa 2017 年 11 月 2 日
Why doesn't what show an error? If you mean why doesn't the compiler complain, it is because the compiler has no clue how many variable pointers are behind the prhs pointer, so it cannot know that prhs[5] is out of bounds.
Deepak Sridhar
Deepak Sridhar 2017 年 11 月 2 日
Yes I meant the compiler. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeWrite 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