Mex Function only returns zero ... and sometimes MATLAB gives system Error.

5 次查看(过去 30 天)
I am trying to run the Mex function from MATLAB but the mex function only returns 0, but i am expecting a combination of 0s and 1s. everything runs fines when i debug but the out put variable u doesn't get the proper values. this is what happens ... for the first iteration it gets the correct value like u[0]= correct value, for next iteration its gets the correct value as well but at wrong location u[0] and not at u[1] ... same goes for u[2] and so on. I want it to return an array like this u[ 0 1 0 1 0 0 1 0]. I have looked up this issue at
https://se.mathworks.com/matlabcentral/answers/32337-array-of-int-at-the-output-of-a-mex-file
but it didnt help. below is the c++ code. any idea what i might be doing wrong ?
This is how i call the mex function in MATLAB uhat = mex_pdecode(N0,EbN0dB,y,FZlookup);
#include <mex.h>
#include <cmath>
#include<bitset>
using namespace std;
const int N=16;
const int K=8;
const int n=4;
double LLR[2*N-1];
int BITS[2][N-1];
//the functions used by the decoder are defined here
double logdomain_diff(double x, double y);
double logdomain_sum(double x, double y);
int bitreversed (int j);
void updateLLR(int i);
void updateBITS(int latestbit,int i);
double lowerconv(int upperdecision, double upperllr, double lowerllr);
double upperconv(double llr1, double llr2);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//inputs from MATLAB
double *N0,*EbN0dB; //1x1 arrays
double *y, *FZlookup; //1xN arrays
//the varibales used by the decoder
double *initialLRs;
int d_hat[N];
int *u;
double N1=N;
double K1=K; int NRow,NRow2;
/*associate inputs*/
N0 = mxGetPr(prhs[0]);
EbN0dB = mxGetPr(prhs[1]);
y = mxGetPr(prhs[2]);
FZlookup = mxGetPr(prhs[3]);
NRow = mxGetM(prhs[2]);
NRow2 = mxGetM(prhs[3]);
/*associate outputs*/
plhs[0] = mxCreateNumericMatrix(K, 1,mxINT32_CLASS, mxREAL);
u = (int *) mxGetData(plhs[0]);
//u = (int *) mxMalloc(K*sizeof(int));
double Ec=(K1/N1)*(*N0)*(pow(10,*EbN0dB/10));
//initialize LLR array to zero
for (int j=0; j<2*N-1; j++)
{
LLR[j]=0;
}
//initialize BITS array to zero
for ( int i = 0; i < 2; i++ )
for ( int j = 0; j < N-1; j++ )
{
BITS[i][j]=0;
//cout << BITS[i][j]<<endl;//Debug
}
//calculate the initial likelihood ratios and assign it to LLR array
for (int i=0; i<NRow; i++)
{
u[i]=0;
int temp=(N-1)+i;
initialLRs[i] = -1*(4*sqrt(Ec)/(*N0))*y[i];
LLR[temp] = initialLRs[i];
}
//the operations of decoder starts here
for (int j=1; j<=N; j++)
{
int i=0;
i=bitreversed(j-1)+1;
//cout<<i<<endl;//Debug
updateLLR(i);
if (FZlookup[i-1]==-1)
{
if (LLR[0]>0)
d_hat[i-1]=0;
else
d_hat[i-1]=1;
}
else
d_hat[i-1]=FZlookup[i-1];
updateBITS(d_hat[i-1],i);
}
int k=0;
for (int i=0; i<NRow2; i++)
{
if(FZlookup[i]==-1 && k<K)
{
u[k]=d_hat[i];
k=k+1;
}
}
//mxFree(u);
}
  2 个评论
Geoff Hayes
Geoff Hayes 2016-10-6
Shajeel - how are you calling this MEX function? Please include the command line call to it so that we can see the parameters that you are passing into it.
Shajeel Iqbal
Shajeel Iqbal 2016-10-7
Geoff i updated the code ... and i have added the line with which i call the mex function in MATLAB.

请先登录,再进行评论。

回答(2 个)

James Tursa
James Tursa 2016-10-6
You don't show enough code for me to determine with certainty what is going on. For instance, the following are not defined anywhere, so I assume they are at the top level (maybe in a header file?): K, N, LLR, BITS, bitreversed, updateLLR, updateBITS. But because I can't see how they are defined (type, size, macro, etc), it is impossible for me to know if they are being misused or not. If you post this code I could have a better shot at determining what is going on. Also, you don't show how you are calling the routine.
That being said, here is what I can see in your code:
double *initialLRs;
:
initialLRs[i] = -1*(4*sqrt(Ec)/(*N0))*y[i];
LLR[temp] = initialLRs[i];
I can't find anywhere in your code where you set initialLRs to a valid memory address. So at the downstream point in the code where you access initialLRs[i], the pointer initialLRs contains garbage and you are writing to a random invalid memory address. This could either produce unpredictable results or crash MATLAB.
  2 个评论
Shajeel Iqbal
Shajeel Iqbal 2016-10-7
James I have added the rest of the code. I am not an expert in C++ and this is my first time writing a mex function. How do i initialize it to valid address ?
James Tursa
James Tursa 2016-10-7
编辑:James Tursa 2016-10-7
Add this line after you have defined the value for nRow:
initialLRs = (double *) mxMalloc( nRow * sizeof(double) );
Then at the end of the routine:
mxFree(initialLRs);

请先登录,再进行评论。


Jan
Jan 2016-10-7
Do not rely on int beeing a 32 bit integer:
int *u;
plhs[0] = mxCreateNumericMatrix(K, 1,mxINT32_CLASS, mxREAL);
u = (int *) mxGetData(plhs[0]);
Better:
int32_T *u;
plhs[0] = mxCreateNumericMatrix(K, 1,mxINT32_CLASS, mxREAL);
u = (int32_T *) mxGetData(plhs[0]);
If you compare a double, use doubnle constants with a dot:
if (FZlookup[i-1] == -1.0)
What is the purpose of "updateLLR(i);" if it does not return anything?

类别

Help CenterFile Exchange 中查找有关 Write 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