image thumbnail
from intbitcopy and uintbitcopy by James Tursa
Exact bit copy of variable into signed int__ or unsigned uint__ of same element size

intbitcopy.m
%/*************************************************************************************
% *
% * MATLAB (R) is a trademark of The Mathworks (R) Corporation
% *
% * Function:    intbitcopy
% * Filename:    intbitcopy.c
% * Programmer:  James Tursa
% * Version:     1.0
% * Date:        November 13, 2007
% * Copyright:   (c) 2007 by James Tursa
% * Permission:  Permission is granted to freely distribute and use this code
% *              as long as the header information is included.
% *
% * intbitcopy is a mex function that copies arguments into signed integer
% * copies of the same byte length using an exact bit copy. Variable types
% * are copied as follows:
% *
% *   double  --> int64
% *   single  --> int32
% *   uint64  --> int64
% *   int64   --> int64
% *   uint32  --> int32
% *   int32   --> int32
% *   uint16  --> int16
% *   int16   --> int16
% *   char    --> int16
% *   uint8   --> int8
% *   int8    --> int8
% *   logical --> int8
% *
% * For structures and cell arrays, each component or cell is individually
% * converted. Other classes (e.g., vpa, function handles, etc.) are not
% * converted; a deep copy is returned instead. intbitcopy takes any number
% * of input arguments; there must be the same number of matching output
% * arguments. intbitcopy is similar to typecast, except that individual
% * element data length is preserved. Also, intbitcopy will copy the
% * complex portion of variables as well as the real portion.
% *
% * Building:
% *
% * >> mex -setup
% *   (then follow instructions to select a C compiler of your choice)
% * >> mex intbitcopy.c
% *
% * Syntax:
% *
% *   Y = intbitcopy(X)
% *   [Y1 Y2] = intbitcopy(X1,X2)
% *   [Y1 Y2 Y3] = intbitcopy(X1,X2,X3)
% *       :             :
% *      etc           etc
% ****************************************************************************/

Contact us at files@mathworks.com