How to Use the table2array() Function in MATLAB for Converting a Table to a Matrix?
The table2array() is a built-in function in MATLAB that allows us to convert a table to a 2-d homogenous array or a matrix. This function accepts the table T as an argument and returns a matrix containing the table data in the matrix form. The function follows a simple syntax that is given below:
Here:
The expression A = table2array(T) creates an array A whose type depends on the contents of T. The sizes and types of each variable in T must be compatible with the horizontal concatenation.
- If T is an M by N table and each variable has its own column, then A will have a column for each variable in T.
- If T represents a table having row names, then A excludes the names of the rows.
- If T represents a timetable, then a excludes the row timings.
Example
This MATLAB code first creates a table having numeric data and then convert it into a matrix using the table2array() function.
'VariableNames',["One" "Two" "Three"])
A = table2array(T)
Conclusion
In MATLAB, we deal with both a table and a matrix for storing the data in the form of rows and columns. A table and a matrix are almost similar, but a matrix must have all the elements of the same data type however, a table can have different types of data but all the elements in a column must be of the same data type. MATLAB supports the conversion of a table to a matrix using a built-in table2array() function. This tutorial described the use of the table2array() function for converting a table to a matrix.