Reorient table or timetable so that rows become variables
T2 = rows2vars(
reorients the rows of
T1
)T1
, so that they become variables in the output table
T2
, as shown in the diagram. If
rows2vars
can concatenate the contents of the rows of
T1
, then the corresponding variables of T2
are arrays. Otherwise, the variables of T2
are cell arrays.
rows2vars
always returns a table, though
T1
can be either a table or a timetable.
The rows2vars
function copies the names of the variables of
T1
to a new variable of T2
. If
T1
has row names or row times, then those names or times
become the variable names of T2
. Otherwise,
rows2vars
generates names Var1,…,VarN
as
the variable names of T2
.
T2 = rows2vars(
specifies additional arguments using one or more name-value pair arguments. For
example, you can use the T1
,Name,Value
)'VariableNamesSource'
name-value pair
argument to specify the source of the variable names of
T2
.
Create tables, and then reorient their rows to be variables in new tables.
Load arrays of data from the patients.mat
file. Create a table that contains the LastName
, Gender
, Age
, Height
, and Weight
variables.
load patients
T1 = table(LastName,Gender,Age,Height,Weight);
head(T1,3)
ans=3×5 table
LastName Gender Age Height Weight
____________ __________ ___ ______ ______
{'Smith' } {'Male' } 38 71 176
{'Johnson' } {'Male' } 43 69 163
{'Williams'} {'Female'} 38 64 131
Reorient the rows of T1 to be the variables of the output table.
T2 = rows2vars(T1);
Display the first four variables of T2
. The first variable of T2
contains the names of the variables of T1
. The remaining variables of T2
correspond to rows of T1
. Since T1
did not have any row labels, the variables of T2
have default names, Var1
to VarN
for N
variables.
T2(:,1:4)
ans=5×4 table
OriginalVariableNames Var1 Var2 Var3
_____________________ _________ ___________ ____________
{'LastName'} {'Smith'} {'Johnson'} {'Williams'}
{'Gender' } {'Male' } {'Male' } {'Female' }
{'Age' } {[ 38]} {[ 43]} {[ 38]}
{'Height' } {[ 71]} {[ 69]} {[ 64]}
{'Weight' } {[ 176]} {[ 163]} {[ 131]}
Create a table with row names. If a table has row names, then rows2vars
turns the row names into the names of variables.
T3 = table(Gender,Age,Height,Weight,'RowNames',LastName);
head(T3,3)
ans=3×4 table
Gender Age Height Weight
__________ ___ ______ ______
Smith {'Male' } 38 71 176
Johnson {'Male' } 43 69 163
Williams {'Female'} 38 64 131
Reorient the rows of T3
.
T4 = rows2vars(T3); T4(:,1:4)
ans=4×4 table
OriginalVariableNames Smith Johnson Williams
_____________________ ________ ________ __________
{'Gender'} {'Male'} {'Male'} {'Female'}
{'Age' } {[ 38]} {[ 43]} {[ 38]}
{'Height'} {[ 71]} {[ 69]} {[ 64]}
{'Weight'} {[ 176]} {[ 163]} {[ 131]}
Load a timetable and display it.
load bostonTT
Boston
Boston=6×3 timetable
Time Temp WindSpeed Rain
___________________ ____ _________ ____
2016-06-09 06:03:00 59.5 0.1 0.05
2016-06-09 12:00:23 63 2.3 0.08
2016-06-09 18:02:57 61.7 3.1 0.13
2016-06-10 06:01:47 55.4 5.7 0.15
2016-06-10 12:06:00 62.3 2.6 0.87
2016-06-10 18:02:57 58.8 6.2 0.33
Reorient it so that its rows become variables in the output. The rows2vars
function turns the row times into names, but modifies them so that they are valid variable names. Also, the output argument returned by rows2vars
is always a table, even when the input argument is a timetable.
T = rows2vars(Boston)
Warning: Table variable names that were not valid MATLAB identifiers have been modified. Since table variable names must be unique, any table variable names that happened to match the new identifiers also have been modified. To use the original row names as new variable names, set 'PreserveVariableNames' to true.
T=3×7 table
OriginalVariableNames x2016_06_0906_03_00 x2016_06_0912_00_23 x2016_06_0918_02_57 x2016_06_1006_01_47 x2016_06_1012_06_00 x2016_06_1018_02_57
_____________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________
{'Temp' } 59.5 63 61.7 55.4 62.3 58.8
{'WindSpeed'} 0.1 2.3 3.1 5.7 2.6 6.2
{'Rain' } 0.05 0.08 0.13 0.15 0.87 0.33
Starting in R2020a, you can take names from the input table or timetable and use them as variable names in the output table without modification. To preserve the original names, use the 'VariableNamingRule'
name-value pair argument.
Load a timetable and display it.
load bostonTT
Boston
Boston=6×3 timetable
Time Temp WindSpeed Rain
___________________ ____ _________ ____
2016-06-09 06:03:00 59.5 0.1 0.05
2016-06-09 12:00:23 63 2.3 0.08
2016-06-09 18:02:57 61.7 3.1 0.13
2016-06-10 06:01:47 55.4 5.7 0.15
2016-06-10 12:06:00 62.3 2.6 0.87
2016-06-10 18:02:57 58.8 6.2 0.33
Reorient the timetable so that its row times become variable names in the output table. Convert the datetime
values to strings and preserve the resulting names using the 'VariableNamingRule'
name-value pair.
T = rows2vars(Boston,'VariableNamingRule','preserve')
T=3×7 table
OriginalVariableNames 2016-06-09 06:03:00 2016-06-09 12:00:23 2016-06-09 18:02:57 2016-06-10 06:01:47 2016-06-10 12:06:00 2016-06-10 18:02:57
_____________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________
{'Temp' } 59.5 63 61.7 55.4 62.3 58.8
{'WindSpeed'} 0.1 2.3 3.1 5.7 2.6 6.2
{'Rain' } 0.05 0.08 0.13 0.15 0.87 0.33
The variable names in T
are not valid MATLAB identifiers because the dates start with a number. However, you can use dot notation to refer to such variables, using parentheses.
T.('2016-06-09 06:03:00')
ans = 3×1
59.5000
0.1000
0.0500
Read data from a spreadsheet into a table. Display the first three rows.
T1 = readtable('patients.xls');
head(T1,3)
ans=3×10 table
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus
____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________
{'Smith' } {'Male' } 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'}
{'Johnson' } {'Male' } 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' }
{'Williams'} {'Female'} 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' }
Reorient the rows of T1
to be variables of a new table, T2
. Specify that the LastName
variable from T1
is the source of the names of the variables of T2
.
T2 = rows2vars(T1,'VariableNamesSource','LastName');
Display the first four variables of T2
. The first variable of T2
contains the names of the variables of T1
. The remaining variables of T2
correspond to rows of T1
.
T2(:,1:4)
ans=9×4 table
OriginalVariableNames Smith Johnson Williams
____________________________ ___________________________ _______________ _____________________________
{'Gender' } {'Male' } {'Male' } {'Female' }
{'Age' } {[ 38]} {[ 43]} {[ 38]}
{'Location' } {'County General Hospital'} {'VA Hospital'} {'St. Mary's Medical Center'}
{'Height' } {[ 71]} {[ 69]} {[ 64]}
{'Weight' } {[ 176]} {[ 163]} {[ 131]}
{'Smoker' } {[ 1]} {[ 0]} {[ 0]}
{'Systolic' } {[ 124]} {[ 109]} {[ 125]}
{'Diastolic' } {[ 93]} {[ 77]} {[ 83]}
{'SelfAssessedHealthStatus'} {'Excellent' } {'Fair' } {'Good' }
Display the data in T2.Smith
. In this example, every variable of T2
is a 9-by-1 cell array, because the values in the rows of T1
cannot be concatenated into arrays.
T2.Smith
ans=9×1 cell array
{'Male' }
{[ 38]}
{'County General Hospital'}
{[ 71]}
{[ 176]}
{[ 1]}
{[ 124]}
{[ 93]}
{'Excellent' }
Read data from a spreadsheet into a table. Use the first column of the spreadsheet as the row names of the table. Display the first three rows.
T1 = readtable('patients.xls','ReadRowNames',true); head(T1,3)
ans=3×9 table
Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus
__________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________
Smith {'Male' } 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'}
Johnson {'Male' } 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' }
Williams {'Female'} 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' }
Reorient specified variables from T1
and discard the rest. To specify data variables by name, use a cell array of character vectors.
T2 = rows2vars(T1,'DataVariables',{'Gender','Age','Height','Weight'}); T2(:,1:4)
ans=4×4 table
OriginalVariableNames Smith Johnson Williams
_____________________ ________ ________ __________
{'Gender'} {'Male'} {'Male'} {'Female'}
{'Age' } {[ 38]} {[ 43]} {[ 38]}
{'Height'} {[ 71]} {[ 69]} {[ 64]}
{'Weight'} {[ 176]} {[ 163]} {[ 131]}
You also can specify data variables by position in the input table. To specify positions of variables, use a numeric array.
T3 = rows2vars(T1,'DataVariables',[1 2 6:9]);
T3(:,1:4)
ans=6×4 table
OriginalVariableNames Smith Johnson Williams
____________________________ _____________ ________ __________
{'Gender' } {'Male' } {'Male'} {'Female'}
{'Age' } {[ 38]} {[ 43]} {[ 38]}
{'Smoker' } {[ 1]} {[ 0]} {[ 0]}
{'Systolic' } {[ 124]} {[ 109]} {[ 125]}
{'Diastolic' } {[ 93]} {[ 77]} {[ 83]}
{'SelfAssessedHealthStatus'} {'Excellent'} {'Fair'} {'Good' }
T1
— Input tableInput table, specified as a table or timetable.
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
'VariableNamingRule','preserve'
preserves original
names taken from T1
, without modifying them to be valid
MATLAB® identifiers.'VariableNamesSource'
— Variable in T1
that contains variable namesVariable in T1
that contains variable names,
specified as the comma-separated pair consisting of
'VariableNamesSource'
and a character vector,
string scalar, positive integer, or logical vector. The
rows2vars
function interprets the value of
'VariableNamesSource'
as shown in the
table.
Value of
| Meaning |
---|---|
Character vector or string scalar | Name of a variable in the input table
|
Integer | The n th variable in
T1 . |
Logical vector, whose length equals the number
of variables in | The |
While the value of 'VariableNamesSource'
must be a
name, number, or logical array that specifies a table variable, the
variable itself can have any data type, with these limitations.
The values contained in the specified table variable must have
a data type that allows the values to be converted to strings.
For example, the value of
'VariableNamesSource'
can be the name of
a table variable that contains a datetime
array, because datetime
values can be
converted to strings.
The number of names taken from the specified table variable must match the number of rows of the input table.
'DataVariables'
— Selected variables from T1
Selected variables from T1
, specified as the
comma-separated pair consisting of 'DataVariables'
and a character vector, cell array of character vectors, string array,
positive integer, vector of positive integers, logical vector, or
subscript object. The rows2vars
function selects
the variables specified by the value of
'DataVariables'
and reorients only those
variables to become the rows of T2
. The remaining
variables of T1
are discarded.
'VariableNamingRule'
— Rule for naming variables in T2
'modify'
(default) | 'preserve'
Rule for naming variables in T2
, specified as the
comma-separated pair consisting of
'VariableNamingRule'
and either the value
'modify'
or 'preserve'
.
The values of 'VariableNamingRule'
specify the
following rules for naming variable in the output table or
timetable.
Value of
| Rule |
---|---|
| Modify names taken from the input table or timetable so that the corresponding variable names in the output are also valid MATLAB identifiers. |
| Preserve original names taken from the input table or timetable. The corresponding variable names in the output can have any Unicode® characters, including spaces and non-ASCII characters. Note: In some cases,
|
You have a modified version of this example. Do you want to open this example with your edits?