From: John Boone on
To Whomever,

I apologize in advance for the length of this post. However, I think you will find it necessary.

I am trying to create a MEX function that takes in a couple of matrices as inputs and does stuff to them before outputting other values to the original Matlab function that called the MEX function. My problem occurs when the inputs are extracted with the mxGetData() function. A condensed version of my code looks like this:

void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// declare variables
double *G, *X, *P, *sigma;
bool *Opt;

// check proper number of arguments
if (nrhs != 5)
mexErrMsgTxt("Incorrect number of inputs");
if (nlhs != 4)
mexErrMsgTxt("Incorrect number of outputs");

// check out input parameters
if (!mxIsDouble(prhs[0]))
mexErrMsgTxt("Error - G must be a double");}
if (!mxIsDouble(prhs[1]))
mexErrMsgTxt("Error - X must be a double");}

// extract inputs
G = (double *)mxGetData(prhs[0]);
X = (double *)mxGetData(prhs[1]);

<do stuff>

}

My problem is that on the Matlab side of things, a simple example of G may look something like this:

..47 .34 .25 1 0 0 0 0 0
..89 .34 .58 0 1 0 0 0 0
..63 .26 .95 0 0 1 0 0 0
..38 .27 .27 0 0 0 0 0 -1
..59 .72 .64 0 0 1 0 0 0
..83 .98 .52 0 0 0 1 0 0
..13 .63 .29 0 0 0 0 1 0

And in the MEX function, I will get this:

..47 .34 .25 .89 .34 .58 .63 .26 .95
..38 .27 .27 .59 .72 .64 .83 .98 .52
..13 .63 .29 -1 0 0 1 0 0
0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0

There are some things to note about the two matrices above.

First, the dimensions are the same in Matlab and in the MEX function (7-by-9 in this example). So although the second matrix might look, at a glance, like the first matrix but flipped over on its side, the two matrices are not transposes of each other.

Second, although there are a lot of zeros in the matrices above (and in the real matrices in my programs), mxIsSparse(prhs[0]) is false.

Third, the second matrix, X, gets extracted with no mistakes, so I do not think this is column-ordering vs. row-ordering or something similar. However, the matrix X is a full double matrix.

Also, I'm using the 64-bit version of Matlab 2009a on a windows platform with Microsoft Visual C++ 2008 as my compiler. I've done some research on the internet to try to find a cause/solution or even someone who has a similar problem, but so far I've turned up nothing. Does anyone have any clue what's going on here?

TIA,
J Boone