Code covered by the BSD License  

Highlights from
typecast and typecastx C-mex functions

5.0

5.0 | 1 rating Rate this file 19 Downloads (last 30 days) File Size: 14.77 KB File ID: #17476
image thumbnail

typecast and typecastx C-mex functions

by James Tursa

 

10 Nov 2007 (Updated 20 Mar 2011)

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 submission has inspired the following:
num2vpi - Converts input exactly into vpi, DataHash
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  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
23 Mar 2011 Jan Simon

Especially the conversion from CHAR is very helpful.

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?

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.

Please login to add a comment or rating.
Updates
12 Feb 2008

I am adding an m-file so that "help typecast" will work.

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.

Tag Activity for this File
Tag Applied By Date/Time
external interface James Tursa 22 Oct 2008 09:34:26
typecast James Tursa 22 Oct 2008 09:34:26
cast James Tursa 22 Oct 2008 09:34:26
mex James Tursa 22 Oct 2008 09:34:26
external code interfacing James Tursa 22 Oct 2008 09:34:26

Contact us at files@mathworks.com