MATLAB: Problems when comparing mwSize and mwIndex variables

mexmwindexmwsize

Hello
I am slowly changing my old fortran-c codes to the new matlab standard. When changing one of the gateway funtions, I encounter the following
….
mwIndex nyt, nsuby;
mwSize YDA_COLS;
….
then nyt and YDA_COLS = nsuby = mxGetN(YDA_IN) take value 1, that is nyt=1 and YDA_COLS=1. When I issue the following comand
if (YDA_COLS < nyt)
{
mexErrMsgTxt("Number of Columns of Y is incompatible. ");
}
The number os Columns of Y is incompatible! I have no idea of what is wrong. If I change to mwSize nyt, nsuby, the same thing happens. It only works if I change to int nyt, nsuby.
Many thanks
Ed

Best Answer

A couple of observations:
>> typecast(uint64(1),'uint8')
ans =
1 0 0 0 0 0 0 0
>> typecast(uint64(4294967297),'uint8')
ans =
1 0 0 0 1 0 0 0
So now I am thinking it may be a function signature problem. If a function is returning a value of 1 as an int (4-bytes), but you told the compiler it is returning an 8-byte integer, then an extra garbage 4-bytes may be picked up by the compiler and stuffed into nyt, accounting for that extra 1 in the bit pattern. This is just speculation, but I have seen this behavior before when prototypes/signatures are not correct.
I next would like to see the exact code where nyt is being set. And if your functions are involved, the prototypes/signatures of those functions.
Are you getting any compiler warnings about mismatched pointer types?
Do you have compiled C mex code calling compiled Fortran code? If so, then I would need to see the Fortran function/subroutine signature also. E.g., if it is the Fortran signature you show above, then I would expect all of the integers on the Fortran side to be 4-bytes. And if you are passing in pointers to 8-byte integers from the C-mex side then yes that would be a problem.