This is machine translation

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


Create table from workspace variables


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


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.


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.


collapse all

Create Table from Workspace Variables

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,...
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 Table and Specify 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],...
    '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.

Build Table by Assigning Variables Individually

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

var1,...,varN — Input variablesarrays

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.

'RowNames' — Row names{} (default) | cell array of character vectors

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.

'VariableNames' — Variable namescell array of character vectors

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

T — Output tabletable

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.

Introduced in R2013b

Was this topic helpful?