Programming and 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:
double
function.
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.
Special Values | Operators |