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

expand 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,...
    '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 Table and Specify Variable Names

Create a table from arrays with different data types.

T = table(['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

Each variable in T contains 3 rows.

Gender is a character array, Age is a double-precision array, State is a cell array of strings, 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.

Input Arguments

expand all

var1,...,varN — Input variablesarrays with the same number of rows

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. Furthermore, input variables can be objects that are arrays that 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 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 4 rows, but different sizes.

Example: table([1:3]',{'one';'two';'three'},['A';'B';'C']) creates a table from variables with 3 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 nonempty, distinct strings

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

'VariableNames' — Variable namescell array of nonempty, distinct strings

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

Output Arguments

expand 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 them automatically. If an input argument is a variable in the current workspace, table uses that variable's name as the table variable name. Otherwise, it generates a string from Var followed by an integer, such as Var2.

Was this topic helpful?