File Exchange

image thumbnail

typecast and typecastx C-mex functions

version 1.3 (14.8 KB) by

typecast and typecastx are mex functions that mimic the MATLAB typecast function with extensions



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.

Comments and Ratings (4)

Daniel O'Shea

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

James Tursa (view profile)

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.

Jan Simon

Jan Simon (view profile)

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?

Jan Simon

Jan Simon (view profile)

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.

MATLAB Release
MATLAB 7.2 (R2006a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video