rows2vars
Reorient table or timetable so that rows become variables
Description
T2 = rows2vars(
reorients the rows of
T1
)T1
, so that they become variables in the output table
T2
. 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. The rows2vars
function copies the names of the variables of T1
to a new
variable of T2
. The output is always a table, though
T1
can be either a table or a timetable.
For example, if T1
has two rows and five variables, then
T2
has five rows, two variables containing the original data
from T1
, and a new variable named
OriginalVariableNames
. In T2
, the variable
OriginalVariableNames
contains the names of the table
variables from T1
.
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
.
Conceptually, you can think of T2
as being like a transpose of
T1
. Technically, T2
is not a true
transpose of the input table because rows2vars
adds a new
variable to T2
. Also, the variables of T1
might have incompatible data types, in which case the "transposed" rows from
T1
become cell arrays in 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
.
Examples
Reorient Rows as Variables
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)
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)
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]}
Reorient Timetable Rows
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 'VariableNamingRule' to 'preserve'.
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
Preserve Original Names
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
Names of Output Table Variables
Read data from a spreadsheet into a table. Display the first three rows.
T1 = readtable('patients.xls');
head(T1,3)
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' }
Specify Data Variables
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)
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' }
Input Arguments
T1
— Input table
table | timetable
Input table, specified as a table or timetable.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=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: 'VariableNamingRule','preserve'
preserves original
names taken from T1
, without modifying them to be valid
MATLAB® identifiers.
VariableNamesSource
— Variable in T1
that contains variable names
character vector | string scalar | positive integer | logical vector
Variable 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 adatetime
array, becausedatetime
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.
The value of 'VariableNamesSource'
cannot specify a
variable that is also specified by the
'DataVariables'
name-value argument.
DataVariables
— Selected variables from T1
string array | character vector | cell array of character vectors | pattern
scalar | positive integer | vector of positive integers | logical vector
Selected variables from T1
, specified as the
comma-separated pair consisting of 'DataVariables'
and a string array, character vector, cell array of character vectors,
pattern
scalar, positive integer, vector of positive
integers, or logical vector. 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.
The value of 'DataVariables'
cannot specify a
variable that is also specified by the
'VariableNamesSource'
name-value argument.
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,
|
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Timetables are not supported.
The input table cannot be variable-size.
The
'VariableNamesSource'
name-value argument is not supported.The value of the
'DataVariables'
name-value argument must be constant.The value of the
'DataVariables'
name-value argument does not support pattern expressions.The value of the
'VariableNamingRule'
name-value argument must be constant.If you assign row names to the input table, then the vector of row names must be constant.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
Version History
Introduced in R2018a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)