TYPECAST and TYPECASTX are mex functions intended to mimic the MATLAB intrinsic typecast function with extensions. These C-mex versions of typecast differs from the intrinsic typecast in the following important aspects:
Type of copy: Deep Data Copy
Allows complex: No
Allows logical: No
Allows char: No
Allows non-vector: No
Type of copy: Shared Data Copy
Allows complex: Yes
Allows logical: Yes (cannot convert from complex)
Allows char: Yes (cannot convert from complex)
Allows non-vector: Yes
Since these C-mex functions produce a shared data copy of the original, it is more efficient than the MATLAB intrinsic typecast, which may be important if you are working with large variables. For non-vector inputs, the first non-singleton dimension must be compatible for the conversion.
There is *NO* difference in functionality between the C-mex typecast and typecastx functions. The purpose of including typecastx is so that users can have the extended functionality shown above without shadowing the intrinsic typecast. e.g., users with older versions of MATLAB that do not have a MATLAB intrinsic typecast might install both typecast and typecastx, whereas users with later versions of MATLAB that *do* have an intrinsic typecast might choose to only install the typecastx function.
Note that these C-mex functions are self-building ... you don't have to know anything about C.
Very nice! For those having some trouble trying to build this on Mac 10.8 or recent versions of Ubuntu, try the following:
run mex -setup and choose mexopts.sh (1 for me)
change CC from gcc to llvm-gcc
change CXX from g++ to llvm-g++
set SDKROOT to '/'
run setenv('LD_LIBRARY_PATH', '/usr/local/lib:/usr/lib:/usr/local/lib64:/usr/lib64')
before running typecastx
Ah, yes. Thanks for pointing this out. I think the issue is the "char *name" which used to be "char name" on older versions. I will put in code to auto-detect this and re-post.
Using this function in Matlab 6.5 leads to a strange behaviour: mx->ClassID must be set to [outclass+1] except for DOUBLEs, which need 2 as ClassID, while mxDOUBLE_CLASS is 6.
Which drawbacks would result from not enabling the temporary and numeric flag?
Especially the conversion from CHAR is very helpful.
Added capability for char and logical types. Added the typecastx files for convenience.
Fixed a sparse matrix bug.
Changed algorithm from deep data copy to shared data copy. Thus the C-mex typecast is more efficient than the MATLAB intrinsic typecast. Also added the capability to handle complex inputs.