File Exchange

image thumbnail

typecast and typecastx C-mex functions

version (14.8 KB) by James Tursa
typecast and typecastx are mex functions that mimic the MATLAB typecast function with extensions


Updated 20 Mar 2011

View License

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:

Intrinsic typecast
Type of copy: Deep Data Copy
Allows complex: No
Allows logical: No
Allows char: No
Allows non-vector: No

C-mex typecast
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.

Cite As

James Tursa (2020). typecast and typecastx C-mex functions (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (8)

doesn't work for me (or am i doing something wrong):
using matlab's typecast:
comp_vector = uint8([126,9,126,9]);
>> typecast(comp_vector,'single')
ans =

using typecastx:
>> typecast(comp_vector,'single')
matlab crashes!!


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.

James Tursa

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:

On Mac OS 10.8:

run mex -setup and choose (1 for me)
edit ~/.matlab/R2012b/
change CC from gcc to llvm-gcc
change CXX from g++ to llvm-g++
set SDKROOT to '/'

On ubuntu:

run setenv('LD_LIBRARY_PATH', '/usr/local/lib:/usr/lib:/usr/local/lib64:/usr/lib64')
before running typecastx

James Tursa

Ah, yes. Thanks for pointing this out. I think the issue is the "char *name" which used to be "char name[64]" 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.

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux