Programming and Data Types    

Data Types

There are 14 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. An additional, user-defined data type, shown below as user class, is a subset of the structure type.

The char data type holds characters in Unicode representation. A character string is merely a 1-by-n array of characters. You can use char to hold an 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, single- and double- precision floating point, and sparse matrices of double-precision. 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.

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.

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.

The following table describes the data types in more detail.

Data Type
Example
Description
single
3*10^38
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
3*10^300
5+6i
Double-precision numeric array. This is the most common MATLAB variable type.
sparse
speye(5)
Sparse double-precision matrix (2-D only). The sparse matrix stores matrices with only a few nonzero elements in a fraction of the space required for an equivalent full matrix. Sparse matrices invoke special methods especially tailored to solve sparse problems.
int8, uint8, int16, uint16, int32, uint32
uint8(magic(3))
Signed and unsigned integer arrays that are 8, 16, and 32 bits in length. Enables you to manipulate integer quantities in a memory efficient manner. These data types cannot be used in mathematical operations.
char
'Hello'
Character array (each character is 16 bits long). This array is also referred to as a string.
cell
{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
a.day = 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.
user class
inline('sin(x)')
MATLAB class. This user-defined class is created using MATLAB functions.
java class
java.awt.Frame
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.
function handle
@humps
Handle to a MATLAB function. A function handle can be passed in an argument list and evaluated using feval.


 Special Values Keywords