2D Input Array Mex
Show older comments
I have 2D Array Input(Prob), everythink is working, Mex was build, but when I debug in C, the values of the array are 0. When I had the 1D Array, the values where right. I have done evrything like in this Example.
#include "mex.h"
//#include <new>
typedef struct _complex1{
double real;
double imag;
} complex1;
double abs_square(complex1 value) {
double abs2;
abs2 = value.real*value.real + value.imag*value.imag;
return abs2;
}
double round(double value) {
double round2;
if (value < 0.5)
{
round2 = 0;
}
else
{
round2 = 1;
}
return round2;
}
/* Input Arguments */
#define QAM_IN prhs[0]
#define Prob_IN prhs[1]
//#define Bit_IN prhs[2]
/* Output Arguments */
#define Prob_QAM_OUT plhs[0]
void nur_hd_c(
complex1 QAM[64],
//int Bit[64],
double Prob[2][6],
//double Prob[6],
double Prob_QAM[64])
{
//double Prob_QAM[64] = {0};
int i;
int hd[6];
const short binary_factor[6] = { 32, 16, 8, 4, 2, 1 };
int constell_indx;
for (i = 0; i<6; i++) {
//hd[i] = round(Prob[1][i]);
hd[i] = (int)round(Prob[1][i]);
//hd[i] = (int)round(Prob[i]);
constell_indx = hd[i] * binary_factor[i] + constell_indx;
}constell_indx = constell_indx + 1;
//Prob_QAM[constell_indx - 1] = Bit[29][0];
//Prob_QAM[constell_indx - 1] = Bit[constell_indx - 1][0];
//Prob_QAM[constell_indx - 1] = Prob[Bit[constell_indx - 1][0]][0];
// for (i = 0; i<64; i++) {
// Prob_QAM[i] = 3 * abs_square(QAM[i]);
// }
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray*prhs[] )
{
complex1 QAM[64]; /*1x64 input matrix */
double Prob[2][6];
//double Prob[6];
//double Bit[64];
size_t ncols; /* size of matrix QAM */
double *bit_in; /* input matrix */
//double * Bit_aux;
//double(*Prob_aux)[2][6];
double ** Prob_aux;
double * QAMR_aux;
double * QAMI_aux;
double * Prob_QAM; /* output matrix */
size_t m, n; /* size of matrix Prob */
int i,j;
//size_t mbit, nbit;
/* check for proper number of arguments */
//if(nrhs!=1) {
// mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","One input required.");
//}
//if(nlhs!=1) {
// mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","One output required.");
//}
/* make sure the first input argument is scalar
if( !mxIsDouble(prhs[0]) ||
mxIsComplex(prhs[0]) ||
mxGetNumberOfElements(prhs[0])!=1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notScalar","Input multiplier must be a scalar.");
}*/
// /* make sure the second input argument is type double */
// if( !mxIsDouble(prhs[0]) ||
// mxIsComplex(prhs[0])) {
// mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble","Input matrix must be type double.");
// }
//
// /* check that number of rows in second input argument is 1 */
// if(mxGetM(prhs[0])!=1) {
// mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector","Input must be a row vector.");
// }
//
/* get the value of the scalar input
multiplier = mxGetScalar(prhs[0]);*/
/* create a pointer to the real data in the input matrix */
QAMR_aux = mxGetPr(QAM_IN);
QAMI_aux = mxGetPi(QAM_IN);
Prob_aux = mxGetPr(Prob_IN);
//Bit_aux = mxGetPr(Bit_IN);
/* get dimensions of the input matrix */
ncols = mxGetN(prhs[0]);
/* create the output matrix */
Prob_QAM_OUT = mxCreateDoubleMatrix(1, (mwSize)ncols, mxREAL);
/* get a pointer to the real data in the output matrix */
Prob_QAM = mxGetPr(Prob_QAM_OUT);
m = mxGetM(Prob_IN);
n = mxGetN(Prob_IN);
/*Allocate the memory for 2D array*/
Prob_aux = mxCalloc(n, sizeof(double *));
for (i = 0; i < n; i++) {
Prob_aux[i] = (double *)mxCalloc(m, sizeof(double));
}
// double **Prob_aux = new double*[6];
// for (int i = 0; i < 6; i++)
// Prob_aux[i] = new double[2];
//mbit = mxGetM(Bit_IN);
//nbit = mxGetN(Bit_IN);
/* Copy the input variables to the local ones */
for (i = 0; i<ncols; i++){
/* Copy the input y to the variable */
QAM[i].real = QAMR_aux[i];
QAM[i].imag = QAMI_aux[i];
}
for (i = 0; i<2; i++){
for (j = 0; j < 6; j++){
/* Copy the input y to the variable */
Prob[i][j] = Prob_aux[i][j];
//Prob[i] = Prob_aux[i];
}
}
//for (i = 0; i<mbit; i++){
//for (j = 0; j < nbit; j++){
/* Copy the input y to the variable */
//Bit[i][j] = Bit_aux[i][j];
//Bit[i] = Bit_aux[i];
//}
//}
/* call the computational routine */
//nur_hd_c(QAM,Prob_QAMR, QAMI,(mwSize)ncols);
//nur_hd_c(QAM, Bit, Prob, Prob_QAM);
nur_hd_c(QAM, Prob, Prob_QAM);
}
Answers (1)
James Tursa
on 6 May 2016
Edited: James Tursa
on 6 May 2016
You can't access 2D matrix values from an mxArray variable like you are doing. I.e., these lines are wrong:
double ** Prob_aux;
:
Prob_aux = mxGetPr(Prob_IN);
:
/*Allocate the memory for 2D array*/
Prob_aux = mxCalloc(n, sizeof(double *));
for (i = 0; i < n; i++) {
Prob_aux[i] = (double *)mxCalloc(m, sizeof(double));
}
:
for (i = 0; i<2; i++){
for (j = 0; j < 6; j++){
/* Copy the input y to the variable */
Prob[i][j] = Prob_aux[i][j];
//Prob[i] = Prob_aux[i];
}
}
The problems are:
1) You can't use a (double * *) pointer to point directly at the result of a mxGetPr call. The result of a mxGetPr call returns a pointer to the data (i.e. a (double *) type), not a pointer to a 2nd level of indirection.
2) You wipe out the result of your mxGetPr call anyway with your subsequent mxCalloc call.
3) Your use of Prob_aux[i][j] inside your for loops is simply copying 0's because you used mxCalloc to allocate this memory and you never set it to anything else prior to using it.
4) MATLAB 2D matrix memory is arranged in column-order (like Fortran), but C 2D array memory is arranged in row-order. So in memory, a MATLAB 2D matrix looks like the transpose of a C 2D matrix. To copy from one to the other, you need to essentially do the transposing manually in your code.
I don't have a C compiler at the moment to test this, but you could try the following to fix these problems:
1) Change Prob_aux to a (double *) type and work with this data pointer directly.
double *Prob_aux;
:
Prob_aux = mxGetPr(Prob_IN);
2) Completely get rid of the mxCalloc stuff you have for Prob_aux. I.e., comment out or delete these lines:
// /*Allocate the memory for 2D array*/
// Prob_aux = mxCalloc(n, sizeof(double *));
// for (i = 0; i < n; i++) {
// Prob_aux[i] = (double *)mxCalloc(m, sizeof(double));
// }
3) & 4) Change your rhs for the Prob_aux[i][j] stuff to do the indexing assuming linear access to the values, and calculate the index based on manually transposing the input 2D matrix in the process. E.g.,
for (i = 0; i<2; i++){
for (j = 0; j < 6; j++){
/* Copy the input y to the variable */
Prob[i][j] = Prob_aux[ 2*j + i ]; // <-- assumes row size is 2
}
}
3 Comments
bobo
on 6 May 2016
James Tursa
on 10 May 2016
I don't understand your current problem. Can you elaborate more? E.g., which specific variables look wrong to you? What are they showing and what did you expect?
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!