Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

table

Create table from workspace variables

Syntax

  • T = table(var1,...,varN)
  • T = table(var1,...,varN,Name,Value)
    example
  • T = table

Description

T = table(var1,...,varN) creates a table from the input variables, var1,...,varN . Variables can be of different sizes and data types, but all variables must have the same number of rows.

For more information on creating and using the table data type, see Tables.

example

T = table(var1,...,varN,Name,Value) includes 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.

T = table creates an empty 0-by-0 table.

Examples

collapse all

Define workspace variables with the same number of rows.

LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];

Create a table, T, as a container for the workspace variables.

T = table(Age,Height,Weight,BloodPressure,...
    'RowNames',LastName)
T = 

                Age    Height    Weight    BloodPressure
                ___    ______    ______    _____________

    Smith       38     71        176       124     93   
    Johnson     43     69        163       109     77   
    Williams    38     64        131       125     83   
    Jones       40     67        133       117     75   
    Brown       49     64        119       122     80   

table names the variables with the workspace variable names.

Create a table from arrays with different data types where each variable contains three rows.

T = table(categorical({'M';'F';'M'}),[45;32;34],...
    {'NY';'CA';'MA'},logical([1;0;0]),...
    'VariableNames',{'Gender' 'Age' 'State' 'Vote'})
T = 

    Gender    Age    State    Vote 
    ______    ___    _____    _____

    M         45     'NY'     true 
    F         32     'CA'     false
    M         34     'MA'     false

Gender is a categorical array, Age is a double-precision array, State is a cell array of character vectors, and Vote is a logical array. You can use the function summary to print the data type and other information about the variables in the table.

Starting in R2016b, you can create string arrays with the string function, and add string arrays as table variables.

FlightNum = [1261;547;3489];
Customer = string({'Jones';'Brown';'Smith'});
Date = datetime(2016,12,20:22)';
Rating = categorical({'Good';'Poor';'Fair'});
Comment = string({'Flight left on time, not crowded';...
                  'Late departure, ran out of dinner options';...
                  'Late, but only by half an hour. Otherwise fine.'});
T = table(FlightNum,Customer,Date,Rating,Comment)
T = 

    FlightNum    Customer       Date        Rating                         Comment                     
    _________    ________    ___________    ______    _________________________________________________

    1261         "Jones"     20-Dec-2016    Good      "Flight left on time, not crowded"               
     547         "Brown"     21-Dec-2016    Poor      "Late departure, ran out of dinner options"      
    3489         "Smith"     22-Dec-2016    Fair      "Late, but only by half an hour. Otherwise fine."

To use the text in a string array as row names, convert the string array to a cell array of character vectors. Then create a table with row names.

Customer = cellstr(Customer);
T = table(FlightNum,Date,Rating,Comment,'RowNames',Customer)
T = 

             FlightNum       Date        Rating                         Comment                     
             _________    ___________    ______    _________________________________________________

    Jones    1261         20-Dec-2016    Good      "Flight left on time, not crowded"               
    Brown     547         21-Dec-2016    Poor      "Late departure, ran out of dinner options"      
    Smith    3489         22-Dec-2016    Fair      "Late, but only by half an hour. Otherwise fine."

Create workspace variables containing snowfall totals on different dates at three locations. These variables are row vectors.

Date = {'12/25/11','1/2/12','1/23/12','2/7/12','2/15/12'};
location1 = [20 5 13 0 17];
location2 = [18 9 21 5 12];
location3 = [26 10 16 3 15];

One way to create a table from these variables is to call the table function with the syntax T = table(Date',location1',location2',location3'). Because the workspace variables are row vectors, you must transpose them to put them into the table as column-oriented data. Therefore, the input arguments are expressions, not simple variables. As a result, table creates T with the default variable names Var1, Var2, Var3, and Var4. You can assign more meaningful names to T.Properties.VariableNames after you create T. But, it might be more convenient to create an empty table, and then add variables one at a time with new names.

Create an empty table. Transpose the workspace variables and add them to the table as column vectors. As part of assigning each workspace variable into T, provide a meaningful name for the table variable.

T = table;
T.Date = Date';
T.Natick = location1';
T.Boston = location2';
T.Worcester = location3'
T = 

       Date       Natick    Boston    Worcester
    __________    ______    ______    _________

    '12/25/11'    20        18        26       
    '1/2/12'       5         9        10       
    '1/23/12'     13        21        16       
    '2/7/12'       0         5         3       
    '2/15/12'     17        12        15       

Related Examples

Input Arguments

collapse all

Input variables, specified as arrays with the same number of rows. The input variables can be of different sizes and different data types.

Common input variables are numeric arrays, logical arrays, character arrays, structure arrays, or cell arrays. Input variables also can be objects that are arrays. Such an array must support indexing of the form var(index1,...,indexN), where index1 is a numeric or logical vector that corresponds to rows of the variable var. In addition, the array must implement both a vertcat method and a size method with a dim argument.

Example: table([1:4]',ones(4,3,2),eye(4,2)) creates a table from variables with four rows, but different sizes.

Example: table([1:3]',{'one';'two';'three'},categorical({'A';'B';'C'})) creates a table from variables with three rows, but different data types.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'RowNames',{'row1','row2','row3'} names the rows row1, row2, and row3.

collapse all

Row names, specified as the comma-separated pair consisting of 'RowNames' and a cell array of character vectors that are nonempty and distinct. The number of character vectors must equal the number of rows in the table.

Variable names, specified as the comma-separated pair consisting of 'VariableNames' and a cell array of character vectors that are nonempty and distinct. The number of character vectors must equal the number of variables. The variable names that you assign must be valid MATLAB® variable names. You can determine valid variable names using the function isvarname.

Output Arguments

collapse all

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 Table Properties.

Unless you specify the table variable names using the VariableNames name-value pair argument, the table function generates the variable names automatically. If an input argument is a variable in the current workspace, table uses that variable name as the table variable name. Otherwise, it generates a name from Var followed by an integer, such as Var2.

More About

collapse all

Tall Array Support

This function supports tall arrays with the limitations:

The syntax TT = table(T1,T2,...) constructs a tall table from several tall arrays T1,T2,.... You can use the 'VariableNames' name-value pair to specify variable names.

For more information, see Tall Arrays.

Introduced in R2013b

Was this topic helpful?