Next: , Up: Data Types   [Contents][Index]


3.1 Built-in Data Types

The standard built-in data types are real and complex scalars and matrices, ranges, character strings, a data structure type, and cell arrays. Additional built-in data types may be added in future versions. If you need a specialized data type that is not currently provided as a built-in type, you are encouraged to write your own user-defined data type and contribute it for distribution in a future release of Octave.

The data type of a variable can be determined and changed through the use of the following functions.

: classname = class (obj)
: class (s, id)
: class (s, id, p, …)

Return the class of the object obj, or create a class with fields from structure s and name (string) id.

Additional arguments name a list of parent classes from which the new class is derived.

See also: typeinfo, isa.

: isa (obj, classname)

Return true if obj is an object from the class classname.

classname may also be one of the following class categories:

"float"

Floating point value comprising classes "double" and "single".

"integer"

Integer value comprising classes (u)int8, (u)int16, (u)int32, (u)int64.

"numeric"

Numeric value comprising either a floating point or integer value.

If classname is a cell array of string, a logical array of the same size is returned, containing true for each class to which obj belongs to.

See also: class, typeinfo.

: cast (val, "type")

Convert val to data type type.

Both val and type are typically one of the following built-in classes:

"double"
"single"
"logical"
"char"
"int8"
"int16"
"int32"
"int64"
"uint8"
"uint16"
"uint32"
"uint64"

The value val may be modified to fit within the range of the new type.

Examples:

cast (-5, "uint8")
   ⇒ 0
cast (300, "int8")
   ⇒ 127

Programming Note: This function relies on the object val having a conversion method named type. User-defined classes may implement only a subset of the full list of types shown above. In that case, it may be necessary to call cast twice in order to reach the desired type. For example, the conversion to double is nearly always implemented, but the conversion to uint8 might not be. In that case, the following code will work

cast (cast (user_defined_val, "double"), "uint8")

See also: typecast, int8, uint8, int16, uint16, int32, uint32, int64, uint64, double, single, logical, char, class, typeinfo.

: y = typecast (x, "class")

Return a new array y resulting from interpreting the data of x in memory as data of the numeric class class.

Both the class of x and class must be one of the built-in numeric classes:

"logical"
"char"
"int8"
"int16"
"int32"
"int64"
"uint8"
"uint16"
"uint32"
"uint64"
"double"
"single"
"double complex"
"single complex"

the last two are only used with class; they indicate that a complex-valued result is requested. Complex arrays are stored in memory as consecutive pairs of real numbers. The sizes of integer types are given by their bit counts. Both logical and char are typically one byte wide; however, this is not guaranteed by C++. If your system is IEEE conformant, single and double will be 4 bytes and 8 bytes wide, respectively. "logical" is not allowed for class.

If the input is a row vector, the return value is a row vector, otherwise it is a column vector.

If the bit length of x is not divisible by that of class, an error occurs.

An example of the use of typecast on a little-endian machine is

x = uint16 ([1, 65535]);
typecast (x, "uint8")
⇒ [   1,   0, 255, 255]

See also: cast, bitpack, bitunpack, swapbytes.

: swapbytes (x)

Swap the byte order on values, converting from little endian to big endian and vice versa.

For example:

swapbytes (uint16 (1:4))
⇒   256   512   768  1024

See also: typecast, cast.

: y = bitpack (x, class)

Return a new array y resulting from interpreting the logical array x as raw bit patterns for data of the numeric class class.

class must be one of the built-in numeric classes:

"double"
"single"
"double complex"
"single complex"
"char"
"int8"
"int16"
"int32"
"int64"
"uint8"
"uint16"
"uint32"
"uint64"

The number of elements of x should be divisible by the bit length of class. If it is not, excess bits are discarded. Bits come in increasing order of significance, i.e., x(1) is bit 0, x(2) is bit 1, etc.

The result is a row vector if x is a row vector, otherwise it is a column vector.

See also: bitunpack, typecast.

: y = bitunpack (x)

Return a logical array y corresponding to the raw bit patterns of x.

x must belong to one of the built-in numeric classes:

"double"
"single"
"char"
"int8"
"int16"
"int32"
"int64"
"uint8"
"uint16"
"uint32"
"uint64"

The result is a row vector if x is a row vector; otherwise, it is a column vector.

See also: bitpack, typecast.


Next: , Up: Data Types   [Contents][Index]