Main Content

Fundamental MATLAB Classes

There are many different data types, or classes, that you can work with in MATLAB®. You can build matrices and arrays of floating-point and integer data, characters and strings, logical true and false values, and so on. Function handles connect your code with any MATLAB function regardless of the current scope. Tables, timetables, structures, and cell arrays provide a way to store dissimilar types of data in the same container.

There are 17 fundamental classes in MATLAB. Each of these classes is in the form of a matrix or array. With the exceptions of function handles and tables, this matrix or array is a minimum of 0-by-0 in size and can grow to an n-dimensional array of any size. A function handle is always scalar (1-by-1). A table always has m rows and n variables, where m >= 0 and n >= 0.

The fundamental MATLAB classes are shown in the diagram.

The fundamental MATLAB classes are the logical, string, char, table, cell, struct, and function handle classes, as well as the numeric classes of single, double, int8, int16, int32, int64, uint8, uint16, uint32, and uint64.

Numeric classes in the MATLAB software include signed and unsigned integers, and single- and double-precision floating-point numbers. By default, MATLAB stores all numeric values as double-precision floating point. (You cannot change the default type and precision.) You can choose to store any number, or array of numbers, as integers or as single-precision. Integer and single-precision arrays offer more memory-efficient storage than double-precision.

All numeric types support basic array operations, such as subscripting, reshaping, and mathematical operations.

You can create two-dimensional double and logical matrices using one of two storage formats: full or sparse. For matrices with mostly zero-valued elements, a sparse matrix requires a fraction of the storage space required for an equivalent full matrix. Sparse matrices invoke methods especially tailored to solve sparse problems.

These classes require different amounts of storage, the smallest being a logical value or 8-bit integer which requires only 1 byte. It is important to keep this minimum size in mind if you work on data in files that were written using a precision smaller than 8 bits.

Tables and timetables also support mathematical operations as long as all their variables have numeric data types. For more information, see Direct Calculations on Tables and Timetables. (since R2023a)

The following table describes the fundamental classes in more detail.

Class Name

Documentation

Intended Use

double, singleFloating-Point Numbers
int8, uint8, int16, uint16, int32, uint32, int64, uint64Integers
string, charCharacters and Strings
  • Data types for text.

  • Both data types store characters as Unicode® characters.

  • Support conversions to and from numeric representations.

  • Use either data type with regular expressions.

  • To search for and match text in strings, use pattern objects. (since R2020b)

  • To store multiple strings, use string arrays. You can also store multiple character vectors in cell arrays. However, the recommended way to store text is to use string arrays.

logicalLogical (Boolean) Operations
  • Use in relational conditions or to test state.

  • Can have one of two values: true or false.

  • Also useful in array indexing.

  • Two-dimensional arrays can be sparse.

function_handleFunction Handles
  • Pointer to a function.

  • Enables passing a function to another function.

  • Can also call functions outside usual scope.

  • Use to specify graphics callback functions.

  • Save to MAT-file and restore later.

table, timetableTables, Timetables
  • Tables and timetables are rectangular containers for mixed-type, column-oriented data.

  • Tables have row and variable names that identify contents.

  • Timetables also provide storage for time series data in a table with rows labeled by timestamps. Timetable functions can synchronize, resample, or aggregate timestamped data.

  • Use the properties of a table or timetable to store metadata such as variable units.

  • Manipulation of elements similar to numeric or logical arrays.

  • Access data by numeric or named index.

  • Can select a subset of data and preserve the table container or can extract the data from a table.

structStructures
  • Fields store arrays of varying classes and sizes.

  • Access one or all fields/indices in single operation.

  • Field names identify contents.

  • Method of passing function arguments.

  • Use in comma-separated lists.

  • More memory required for overhead

cellCell Arrays
  • Cells store arrays of varying classes and sizes.

  • Allows freedom to package data as you want.

  • Manipulation of elements is similar to numeric or logical arrays.

  • Method of passing function arguments.

  • Use in comma-separated lists.

  • More memory required for overhead

Related Topics

External Websites