Cheat Sheets

Organizing and Accessing Data in MATLAB

This reference shows common use cases, so it is not an exhaustive list.

Representing Data
Homogenous
Data Type Icon Purpose Syntax
Double, single, (u)int8, (u)int16, (u)int32, (u) int64, complex
Double, single, complex
Numeric arrays, matrix computations, math [1,2,3], [1;2;3], uint8(), int16()
String
String
Text arrays "hello world"
Char
Char
Single characters, character arrays 'hello'
Categorical
Categorical
Discrete, nonnumeric data categorical()
Datetime
Datetime
Absolute dates and timestamps, including time zones datetime('July 12, 2001 08:15:01')
Duration
Duration
Elapsed times duration(h,m,s), hours(), minutes()
Calendar of duration
Calendar of duration
Relative time based on calendar caldays(), calweeks()
Logical
Logical
True/false, test state, identify data by condition logical(), ==, ~=, >, >=, <, <=, &, &&, |, ||
Other specialized types
Other specialized types
sparse, enumeration, custom, … » Documentation
Heterogeneous
Data Type Icon Purpose Syntax
Table
Table
Mixed-type, column-oriented data (spreadsheet-like).
Store metadata
table(x,y,z), array2table
Timetable
Timetable
Timestamped tabular data timetable(t,x,y) table2timetable,array2timetable
Structure
Structure
Fields can contain data of any size and type. Ideal for nonrectangular data. struct()
Cell array
Cell array
Each cell in the array can contain any data type, any size cell(), {pi,ones(5), "hello"}
Tall array
Tall array
MATLAB data types can be made "tall" when data does not fit in memory ds = datastore(), T = tall(ds)
Dictionary
Dictionary
Object that maps unique keys to values d = dictionary(keys,values)
Data Selection
Use array indexing to select data.  
Linear indexing for 1D arrays:
X(1) First element
X(end) Last element
 
 
 
Row, column indexing for multidimensional arrays:
A(1,2)
A(1,1,2)
   
   
Select multiple with a vector:
A([1,3],1)
   
   
   
Use colon : to select a range:
A(1:3,1)
A(:,1) All rows, column 1
A(1,:) Row 1, all columns
A(1:2:end,:) Every other row
   
   
   
Remove data from array:
A(1,:) = [];
Logical Indexing

Use logical expressions to select data

Elements of X greater than 7:
Y = X(X > 7)

Combine conditions
X(X > 3 & X <= 7)
X(X > 3 | X <= 7)

Multidimensional arrays
Use condition to identify rows or columns:
idx = X > 7;
X(idx, :)

Tables and timetables
T(:,vartype('numeric'))
TT(timerange(t1,t2),:)

Container Indexing
Using parentheses () for indexing retains the initial data type. Access the underlying data with curly braces {}. Tables and structures also allow you to reference data by name.

» Examples

Type Subset Contents
Table Returns a table: T(1,2)
T(:,"A")
T(:,["A","B"])
Returns underlying data:
T{1,2}
T{:,"A"} T.A

T.Rows
T.Variables
Timetable Same as above:
TT("Apr 1,... 2004",5)
Same as above:
TT.Time
Cell array Returns a cell:
C(1,2)
C{1,2}
C{:} -> comma separated list
Structure Returns a struct:
S(1,1)
S.Field