Code covered by the BSD License  

Highlights from
typecast and typecastx C-mex functions

5.0

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

typecast and typecastx C-mex functions

by

 

10 Nov 2007 (Updated )

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

| Watch this File

File Information
Description

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.

Acknowledgements

This file inspired Num2vpi Converts Input Exactly Into Vpi 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:
from http://stackoverflow.com/questions/12643236/matlab-2012a-mex-to-work-with-xcode-4-5-on-mountain-lion

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

On ubuntu:
from
http://www.mathworks.com/matlabcentral/answers/47064-running-mex-files-compiled-on-linux
http://stackoverflow.com/a/5963813

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

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

30 Apr 2011 Jan Simon

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?

23 Mar 2011 Jan Simon

Especially the conversion from CHAR is very helpful.

Updates
03 Aug 2009

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

Fixed a sparse matrix bug.

20 Mar 2011

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

Contact us