Programming and Data Types    

Data Types

There are 15 fundamental data types (or classes) in MATLAB. Each of these data types is in the form of an array. This array is a minimum of 0-by-0 in size and can grow to an n-dimensional array of any size. Two-dimensional versions of these arrays are called matrices.

All of the fundamental data types are shown in lowercase text in the diagram below. Additional data types are user-defined, object-oriented user classes (a subclass of structure), and java classes, that you can use with the MATLAB interface to Java.

Matrices of type double and logical may be either full or sparse. For matrices having a small number of nonzero elements, a sparse matrix requires a fraction of the storage space required for an equivalent full matrix. Sparse matrices invoke special methods especially tailored to solve sparse problems.

The logical data type represents a logical true or false value using the numbers 1 and 0, respectively. MATLAB returns logical values from its relational (e.g., >, ~=) and logical (e.g., &&, xor) operations and functions.

The char data type holds characters. A character string is merely a 1-by-n array of characters. You can use char to hold an m-by-n array of strings as long as each string in the array has the same length. (This is because MATLAB arrays must be rectangular.) To hold an array of strings of unequal length, use a cell array.

Numeric data types include signed and unsigned integers, and single- and double- precision floating point numbers. The following hold true for numeric data types in MATLAB:

A cell array provides a storage mechanism for dissimilar kinds of data. You can store arrays of different types and/or sizes within the cells of a cell array. For example, you can store a 1-by-50 char array, a 7-by-13 double array, and a 1-by-1 uint32 in cells of the same cell array. You access data in a cell array using the same matrix indexing used on other MATLAB matrices and arrays.

The MATLAB structure data type is similar to the cell array in that it also stores dissimilar kinds of data. But, in this case, it stores the data in named fields rather than in cells. This enables you to attach a name to the groups of data stored within the structure. You access data in a structure using these same field names.

A function handle holds information to be used in referencing a function. When you create a function handle, MATLAB captures all the information about the function that it needs to locate and execute, or evaluate, it later on. Typically, a function handle is passed in an argument list to other functions. It is then used in conjunction with feval to evaluate the function to which the handle belongs.

MATLAB data types are implemented as classes. You can also create MATLAB classes of your own. These user-defined classes inherit from the MATLAB structure class and are shown in the previous diagram as a subset of structure.

MATLAB provides an interface to the Java programming language that enables you to create objects from Java classes and call Java methods on these objects. A Java class is a MATLAB data type. There are built-in and third-party classes that are already available through the MATLAB interface. You can also create your own Java class definitions and bring them into MATLAB.

The following table describes the data types in more detail.

Data Type
magic(4) > 10

Logical array. Must contain only logical 1 (true) and logical 0 (false) elements. (Any nonzero values converted to logical become logical 1.) Logical matrices (2-D only) may be sparse.

Character array (each character is 16 bits long). This array is also referred to as a string.
int8, uint8, int16, uint16, int32, uint32, int64, uint64
Signed and unsigned integer arrays that are 8, 16, 32, and 64 bits in length. Enables you to manipulate integer quantities in a memory efficient manner. These data types cannot be used in mathematical operations.
Single-precision numeric array. Single precision requires less storage than double precision, but has less precision and a smaller range. This data type cannot be used in mathematical operations.
Double-precision numeric array. This is the most common MATLAB variable type. Double matrices (2-D only) may be sparse.
{17 'hello' eye(2)}
Cell array. Elements of cell arrays contain other arrays. Cell arrays collect related data and information of a dissimilar size together.
structure = 12;
a.color = 'Red';
a.mat = magic(3);

Structure array. Structure arrays have field names. The fields contain other arrays. Like cell arrays, structures collect related data and information together.
function handle
Handle to a MATLAB function. A function handle can be passed in an argument list and evaluated using feval.
user class
MATLAB class. This user-defined class is created using MATLAB functions.
java class
Java class. You can use classes already defined in the Java API or by a third party, or create your own classes in the Java language.

  Special Values Operators