File Exchange

image thumbnail

typecast and typecastx C-mex functions

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

3 Downloads

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 (https://www.mathworks.com/matlabcentral/fileexchange/17476-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 =
single
3.0579e-33

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

Jan

One advantage of typecastx over typecast is that it can handle matrices.

Sven

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:
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

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.

Jan

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

Especially the conversion from CHAR is very helpful.

Updates

1.3.0.0

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

1.2.0.0

Fixed a sparse matrix bug.

1.1.0.0

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.

1.0.0.0

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