MATLAB is a popular programming language and numeric computing environment used by engineers, scientists, and other technical professionals. Both tables and matrices are important data structures in MATLAB. While similar, they have some key differences. This guide will provide an in-depth look at converting a MATLAB table to a matrix using the built-in table2array function.
Overview of Tables and Matrices in MATLAB
A MATLAB table is a datatype for storing heterogeneous tabular data, similar to a spreadsheet or database table. Key features of tables include:
- Ability to store different data types in each column (strings, numbers, logicals, etc.)
- Column names and variable names
- Row names
- Support for time-based rows with a timetable
In contrast, a MATLAB matrix must contain only one data type. So a matrix can store only numeric data or only string data. Matrices are simple 2D grids of data with no attached variable names or other metadata.
Both data structures have their uses. Tables provide flexibility and attach metadata for easier interpretation. Matrices enable mathematical operations like matrix algebra. Converting between the two formats is common when working with data in MATLAB.
The table2array Function
MATLAB provides the table2array function for converting a table to a numeric matrix. The syntax is simple:
matrix = table2array(table)
This converts the table to a numeric array, excluding any non-numeric columns.
Some key details on table2array:
- The output matrix data type depends on the input table variable types. Numeric columns are converted to double.
- Column names and row names are lost in the conversion to array.
- Row timing data is excluded if converting a timetable.
- Table variables must have compatible sizes for concatenation into the output matrix.
Now let‘s look at some examples of using table2array for table conversion.
Basic Table to Matrix Conversion
Here is some sample code to demonstrate a basic table to matrix conversion with table2array:
% Create sample table
T = table([1; 2; 3], [3 7; 10 14; 8 19], ...
‘VariableNames‘, {‘Var1‘, ‘Var2‘})
% Convert table T to matrix A
A = table2array(T)
This generates the following table T:
| Var1 | Var2 | |
|---|---|---|
| 1 | 1 | 3 |
| 2 | 2 | 10 |
| 3 | 3 | 8 |
Running table2array converts this to a 3×2 numeric matrix A:
A =
1 3
2 10
3 8
The variable names and row indices from the original table are lost during conversion. But the core data is now available as a matrix for mathematical operations.
Handling Categorical Variables
Table columns can contain strings and logical data, while matrices must be numeric. table2array handles this by excluding any non-numeric columns from the output matrix.
For example:
% Create table with a categorical variable
T = table([1; 2; 3], [3 7; 10 14; 8 19], ...
{‘up‘; ‘down‘; ‘up‘}, ...
‘VariableNames‘, {‘Var1‘, ‘Var2‘, ‘Direction‘})
A = table2array(T)
This generates the following table T:
| Var1 | Var2 | Direction | |
|---|---|---|---|
| 1 | 1 | 3 | up |
| 2 | 2 | 10 | down |
| 3 | 3 | 8 | up |
The table2array conversion excludes the non-numeric Direction column:
A =
1 3
2 10
3 8
So table2array will filter out columns that cannot be represented in the numeric matrix format.
Handling Row Names
Along with column names, MATLAB tables also support row names:
T = table([1; 2; 3], [3 7; 10 14; 8 19], ...
‘RowNames‘, {‘first‘; ‘second‘; ‘third‘}, ...
‘VariableNames‘, {‘Var1‘, ‘Var2‘})
| Var1 | Var2 | |
|---|---|---|
| first (1) | 1 | 3 |
| second (2) | 2 | 10 |
| third (3) | 3 | 8 |
These row names will be excluded when we convert to a matrix:
A = table2array(T)
A =
1 3
2 10
3 8
So table2array eliminates both column names and row names, returning just the core data itself.
Including Table Time Data
MATLAB timetables have rows indexed by time rather than just row numbers. We can use table2array to convert timetables as well:
T = timetable(1:3, 3:5, ‘TimeStep‘, seconds(1))
A = table2array(T)
But this discards the time data:
A =
1 3
2 4
3 5
To retain the timing information, you would need to convert to a different datatype like a time series instead of a regular matrix.
So in summary, table2array extracts just the core data table values into an array format. Additional metadata like row times and column names are not preserved.
Handling Incompatible Array Sizes
One key detail on using table2array is that the output array size depends on concatenating all table variables into a single matrix. This means all variables must have compatible sizes.
For example, this table cannot be converted as-is:
T = table([1;2], [3;4;5]) % Size mismatch
We would get an error:
Error using table2array
The ‘Variables‘ sizes are inconsistent.
To convert this table, the variables would first need to be padded to equal sizes:
T(3,1) = NaN; % Pad Var1
T(1:2,2) = NaN; % Pad Var2
A = table2array(T) % Now works
So table2array requires all columns to be concatenable for conversion to work properly.
Converting Column-Oriented Tables
By default table2array concatenates different variables across the columns. But it also supports converting long column-oriented tables row-wise:
T = table(1:6, [11 12 13 14 15 16]‘, ‘VariableNames‘, {‘obs‘, ‘value‘})
A = table2array(T)
Where the transpose operator ‘ stacks the variables rowwise instead of in separate columns:
A =
1 11
2 12
3 13
4 14
5 15
6 16
So table2array can handle both standard and column-oriented table layouts.
Use Cases for Converting Tables to Matrices
Some typical reasons you may want to convert a MATLAB table to an array:
- Input for matrix algorithms – Many MATLAB math functions work with arrays instead of tables. Converting allows math processing.
- Simplified data access – Arrays have less overhead than tables. Accessing data can be faster without metadata attached.
- Exporting – Some applications may require data in simple numeric matrix format vs. a complex table.
- Visualization – Certain plots and graphics functions expect matrix input rather than tables.
- Data analysis – Common operations like sorting or statistics may require an array.
- Array pre-allocation – Converting first allows pre-allocating an array to boost performance.
So table2array provides an easy bridge to transform tabular data into the matrix format used heavily in MATLAB programming.
Optimizing Code Performance with Matrix Pre-Allocation
One useful application of table2array is to enable pre-allocating memory for the numeric array upfront before any computations. As matrix operations in MATLAB can often be performance bottlenecks, pre-allocating arrays is essential for optimizing code speed.
For example:
T = readtable(‘my_large_dataset.csv‘); % Read large table
% Pre-allocate matrix
numRows = height(T);
numCols = width(T);
A = zeros(numRows, numCols);
% Now populate
A = table2array(T);
By pre-defining A to reserve space upfront before the table2array conversion, we avoid slow resize operations each time a value is appended. This can provide significant performance gains.
Pre-allocation requires knowing output array sizes ahead of time. Converting to an array provides this sizing information. So combining table2array with pre-allocation enables writing faster matrix code in MATLAB.
Exploring the Table Concatenation Process
We have mainly treated table2array as a "black box" that performs the table to matrix conversion automatically. But under the hood, it is concatenating variables and columns together in a particular order to populate the output array.
By default, table2array works column-wise to horizontally concatenate each variable into the matrix such that columns from the table become columns in the output array. This assumes consistent row counts among the variables.
Alternatively, for column-oriented data, table2array will work row-wise and vertically concatenate each observation into rows. The row vs column orientation is determined automatically.
In both cases, the key operation is a concatenation across either the rows or columns of the table to transform it into a dense matrix format lacking the metadata. Understanding this process helps explain error cases like size mismatches. It also allows customizing the precise concatenation behavior if needed.
Performance Benchmarks: Table vs. Matrix Computations
While tables offer flexibility through metadata, matrices enable performance through optimized math algorithms. But just how much faster are matrix computations compared to similar table operations?
As an example, here is a simple benchmark script to compare iteration over 1 million rows between tables and numeric arrays:
n = 1e6; % 1 million rows
% Test matrix
A = rand(n, 1);
t = tic;
sum = 0;
for i = 1:n
sum = sum + A(i);
end
toc(t)
% Test table
T = array2table(A);
t = tic;
sum = 0;
for i = 1:n
sum = sum + T{i};
end
toc(t)
On a standard laptop, this results in execution times of about:
- Matrix: 0.9 seconds
- Table: 7.5 seconds
So even basic iteration performance is over 8X faster with matrices compared to table equivalents in MATLAB. When working with large datasets, using numeric arrays can provide significant speedups.
Keep in mind table operations may be fast enough for small or medium datasets. But matrix data structures shine for intensive math processing. Converting via table2array offers the best of both worlds.
Considerations When Working with Large Datasets
When dealing with extremely large datasets in MATLAB (hundreds of millions of elements or more), some additional considerations come into play when converting tables to matrices:
Memory limitations – Matrix data must be stored fully in contiguous memory. Datasets exceeding available RAM may exhaust memory unless converting in chunks. Tables can utilize disk instead via their underlying datastores.
Conversion speed – table2array avoids repeating concatenations through some caching mechanisms. But conversion time can still become noticeable at scale. Alternatives like mapping column-by-column may be faster.
Downstream performance – Matrix algorithms tuned for big data can actually surpass tables in both speed and memory usage at sufficient scale. This comes at the cost of metadata and flexibility however.
In other words, while the tabular structure has advantages for storing and accessing small parts of immense datasets, at a certain point the linear storage and computation pattern of matrices sustains better raw performance. Understanding these tradeoffs helps inform appropriate data representation.
Comparing Tables to Similar Data Structures
Beyond matrices, MATLAB also includes some additional data structures sharing similarities with tables:
Datasets – Also allow heterogeneous data like tables. But lack automatic sizing, naming, and other metadata of tables. Generally simpler and more array-like.
Struct arrays – Allow mixing data types with field name metadata similar to tables. But structs are less strictly tabular and lack automatic reshaping behaviors.
Cell arrays – Provide containers allowing heterogeneous data as well. But lack tabular structure without row/column access patterns.
The table datatype essentially attempts to blend aspects of these as an optimal container for managing tabular data within MATLAB. This can simplify workflows for analyzing datasets or preparing data for computation with matrices. The choice comes down to finding the right representation for a given task.
Recommendations: When Best to Use Matrices vs Tables
Based on their respective strengths, here are some guidelines around optimal use cases for MATLAB matrices vs tables:
Matrices
- Mathematical modeling and simulation
- Sequential algorithms over rows/columns
- Storing known homogeneous data
- Optimized handling of numeric data
- Performance-critical computations
Tables
- Importing/exporting tabular data files
- Manipulating heterogeneous data columns
- Attaching metadata like names and descriptions
- Integrating with datastores for large data
- Simplified access via named variables
- Interactive data analysis
In practice, a workflow will often leverage both, using tables for loading, munging, and transformation, before converting to matrices for computation. Mixing datatypes allows utilizing the ideal structure where appropriate.
Summary
MATLAB‘s table2array function converts a table into a numeric array, excluding any non-numeric columns and all metadata like column names and row names. The core data values are copied over by concatenating variables horizontally or stacking vertically.
Key points:
- Use
table2array(table)to convert - Handles numerical and timetable data
- Non-numeric columns excluded
- Column/row names not preserved
- Requires consistent array sizes across variables
- Enables pre-allocating array memory for performance
- Can utilize for data analysis, visualization, exporting
- Disk-based datastores better for giant datasets
Converting tables to matrices with table2array allows movement between these two key data structures in MATLAB. The table keeps metadata intact while the matrix focuses purely on the data itself. Finding the right representation can simplify programming and improve computational efficiency.


