View License

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

» Watch video

Highlights from
typecast and typecastx C-mex functions

5.0 | 2 ratings Rate this file 7 Downloads (last 30 days) File Size: 14.8 KB File ID: #17476 Version: 1.3
image thumbnail

typecast and typecastx C-mex functions


James Tursa (view profile)


10 Nov 2007 (Updated )

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

| Watch this File

File Information

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.


This file inspired Num2vpi Converts Input Exactly Into Vpi, Cachedcall, and Data Hash.

MATLAB release MATLAB 7.2 (R2006a)
Other requirements Any C compiler, such as the built in lcc compiler. (typecast is self-building ... you don't have to know anything about C)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
13 Mar 2013 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

30 Apr 2011 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.

Comment only
30 Apr 2011 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?

Comment only
23 Mar 2011 Jan Simon

Jan Simon (view profile)

Especially the conversion from CHAR is very helpful.

03 Aug 2009 1.1

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.

05 Aug 2009 1.2

Fixed a sparse matrix bug.

20 Mar 2011 1.3

Added capability for char and logical types. Added the typecastx files for convenience.

Contact us