Path: news.mathworks.com!not-for-mail
From: "Akim " <aaa@bbb.ccc>
Newsgroups: comp.soft-sys.matlab
Subject: Re: C mex: working with integer mxArrays
Date: Wed, 6 Jan 2010 09:42:21 +0000 (UTC)
Organization: University of Melbourne
Lines: 63
Message-ID: <hi1ltt$588$1@fred.mathworks.com>
References: <hi0oe8$g72$1@fred.mathworks.com> <hi1eku$irh$1@fred.mathworks.com>
Reply-To: "Akim " <aaa@bbb.ccc>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1262770941 5384 172.30.248.35 (6 Jan 2010 09:42:21 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 6 Jan 2010 09:42:21 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 757091
Xref: news.mathworks.com comp.soft-sys.matlab:596805

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hi1eku$irh$1@fred.mathworks.com>...
> "Akim " <aaa@bbb.ccc> wrote in message <hi0oe8$g72$1@fred.mathworks.com>...
> > Dear all,
> > 
> > I'm new to mexs and C, and I'm trying to figure out the best way of working with Matlab data inside C mex functions. For double and logical Matlab data, the answer seems fairly straightforward if I understand correctly. For example:
> > 
> > double *A;
> > mxLogical *B;
> > 
> > A = mxGetPr(prhs[0]);   /*first input is a double*/
> > B = mxGetPr(prhs[1]);   /*second input is a logical*/
> 
> That is not correct. mxGetPr returns a double *, so it should only be used for this type. The above assignment converts a double * to a mxLogical *, which C will often let you get away with but is bad form. For other types, use mxGetData, which returns a void *. e.g.,
> 
> B = (mxLogical *) mxGetData(prhs[1]);
> 
> > *(A+1)=5;              	/*assign 5 to second entry of A*/
> > *(B+1)=5;              	/*assign 5 to second entry of B*/
> 
> mxLogical type is *likely* a char type, but it may be a bool instead. So this assignment may not do what you expect since 5 may be converted to bool type in this case and not retain the value 5.
> 
> > Now suppose that prhs[2] is of some integer type, e.g. uint32. What is the best way of declaring a pointer to prhs[2]? How do I know whether to use "int", "long int" or "short int"? If I'm not mistaken, sizes of ints vary from system to system.
> > 
> > Thus, I'm looking for an integer equivalent to mxLogical.
> 
> For mxLogical in particular, it is always 1 byte, and in C the char types are always 1 byte also. So you can always use a char type to get at the mxLogical data and have things match up. e.g., the following will always work for a mxLogical prhs[1] variable that has at least 2 elements:
> 
> unsigned char *B;
> B = (unsigned char *) mxGetData(prhs[1]);
> *(B+1)=5;
> 
> For other types, if your compiler does not support the C99 integer size types, you can often figure it out at compile time by using the pre-processor to examine the integer limits in the limints.h header file. e.g., suppose you are looking for a 32-bit integer type to use with MATLAB's uint32 type. You could do something like this:
> 
> #include <limits.h>
> #if UCHAR_MAX == 4294967295
> #define MY32BIT unsigned char
> #elif USHRT_MAX == 4294967295
> #define MY32BIT unsigned short
> #elif UINT_MAX == 4294967295
> #define MY32BIT unsigned int
> #elif ULONG_MAX == 4294967295
> #define MY32BIT unsigned long
> #else
> #define MY32BIT CANT_FIND_32BIT_INT  // Will generate a compile error downstream
> #endif
> 
> And then in your downstream code use MY32BIT as your 32-bit unsigned integer type. There is no guarantee that any of the integer types for a particular processor will have those limits I show above, but if they do then it is very likely they are 32-bit and will match MATLAB's uint32 class. For 32-bit systems, an int is very likely 32-bits.
> 
> James Tursa

Thanks all. Sorry for my bad illustrative example -- I know not to modify prhs or assign 5 to logical arrays.

Dear James,

Thanks for the tip with mxGetData. So if I understand correctly, an unsigned char pointer and an mxLogical pointer are essentially equivalent. Does it matter which one I use? I'm a little confused because elsewhere you said, "Use mxLogical, since MATLAB has supplied that data type for you to use." ( http://www.mathworks.com/matlabcentral/newsreader/view_thread/159081#400659 ).

Dear Ashish,

> A 'short' in C/C++ is always 16-bit while a 'long' is 32-bit. The only
> exception would be when your platform's smallest data type is say 32-
> bit. 

I just cross-checked on two platforms: long is 32-bit on one platform, and 64-bit on the other. Could you clarify?