array2table - Convert homogeneous array to table - MATLAB (original) (raw)
Convert homogeneous array to table
Syntax
Description
[T](#btx3z8%5F-1%5Fsep%5Fshared-T) = array2table([A](#btx3z8%5F-1-A))
converts anm
-by-n
array to anm
-by-n
table. Each column of inputA
becomes a variable in output T
.
array2table
uses the input array name appended with the column number for the variable names in the table. If these names are not valid MATLAB® identifiers, array2table
uses names of the form'Var1',...,'Var_`N`_'
, where_N
_ is the number of columns inA
.
[T](#btx3z8%5F-1%5Fsep%5Fshared-T) = array2table([A](#btx3z8%5F-1-A),[Name,Value](#namevaluepairarguments))
creates a table from an array, A
, with additional options specified by one or more Name,Value
pair arguments.
For example, you can specify row names or variable names to include in the table.
Examples
Create an array of numeric data.
A = [1 4 7; 2 5 8; 3 6 9]
A = 3×3
1 4 7
2 5 8
3 6 9
Convert the array, A
, to a table.
T=3×3 table A1 A2 A3 __ __ __
1 4 7
2 5 8
3 6 9
The table has variable names that append the column number to the input array name, A
.
Create an array of numeric data.
A = [1 12 30.48; 2 24 60.96; 3 36 91.44]
A = 3×3
1.0000 12.0000 30.4800
2.0000 24.0000 60.9600
3.0000 36.0000 91.4400
Convert the array, A
, to a table and include variable names.
T = array2table(A,... 'VariableNames',{'Feet','Inches','Centimeters'})
T=3×3 table Feet Inches Centimeters ____ ______ ___________
1 12 30.48
2 24 60.96
3 36 91.44
Input Arguments
Input array, specified as a matrix.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| cell
Complex Number Support: Yes
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: 'RowNames',{'row1','row2','row3'}
uses the row names,row1
, row2
, and row3
for the table, T
.
Row names, specified as a cell array of character vectors or string array, whose elements are nonempty and distinct. The number of row names must equal the number of rows of the input array.
Row names can have any Unicode® characters, including spaces and non-ASCII characters, except for ':'
.
If you specify row names that have leading or trailing whitespace characters, then array2table
removes them from the row names.
Variable names, specified as a cell array of character vectors or a string array, whose elements are nonempty and distinct. The number of variable names must equal the number of columns of the input array.
Variable names can have any Unicode characters, including spaces and non-ASCII characters. However, a variable name cannot match any table dimension name or the reserved names 'Properties'
, 'RowNames'
, 'VariableNames'
, or ':'
.
Since R2021a
Dimension names, specified as a two-element cell array of character vectors or two-element string array whose elements are nonempty and distinct.
Dimension names can have any Unicode characters, including spaces and non-ASCII characters. However, a dimension name cannot match any table variable name or the reserved names'Properties'
, 'RowNames'
,'VariableNames'
, or ':'
.
As an alternative, in all releases you can specify dimension names by setting theDimensionNames
property of the table.
Output Arguments
Output table, returned as a table. The table can store metadata such as descriptions, variable units, variable names, and row names. For more information, see the Properties section of table.
Tips
- If
A
is a cell array, usecell2table(A)
to create a table from the contents of the cells inA
. Each variable in the table is numeric or a cell array of character vectors.array2table(A)
creates a table where each variable is a column of cells.
Extended Capabilities
This function supports tall arrays with the limitations:
The 'RowNames'
name-value pair is not supported.
For more information, see Tall Arrays.
Version History
Introduced in R2013b
Table and timetable variable names with leading or trailing whitespace characters are not modified.
In previous releases, leading and trailing whitespace characters were deleted from variable names when you specified them using the 'VariableNames'
name-value pair argument, or assigned them to the VariableNames
property.
To manually remove such characters, first use the strtrim function on the names, then assign them as variable names to the table or timetable.