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.
James Tursa (2020). typecast and typecastx C-mex functions (https://www.mathworks.com/matlabcentral/fileexchange/17476-typecast-and-typecastx-c-mex-functions), MATLAB Central File Exchange. Retrieved .
doesn't work for me (or am i doing something wrong):
using matlab's typecast:
comp_vector = uint8([126,9,126,9]);
One advantage of typecastx over typecast is that it can handle matrices.
Thanks James, and thanks also for pointing out the functionality post-R2014b - this saves me spending time to set up some tests to see what differences I'd get using 2018a.
Note that as of R2014b, MATLAB typecast does shared data copies so typecastx becomes largely obsolete (except for the logical and char capability).
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.
I am adding an m-file so that "help typecast" will work.