Matlab

What is the table Function in MATLAB?

A table() is a built-in MATLAB function used for arranging the data in tabular or column-oriented form. It stores each piece of the data as a variable and all variables must have an equal number of elements in each column. These variables are not only restricted to storing column vectors, they can store a matrix as well.

Syntax for table() Function

The table() function has several syntaxes, which are given below:

T = table(var1,…,varN)

T = table(___,’VariableNames’,varNames)

T = table(___,’RowNames’,rowNames)

Here:

T = table(var1,…,varN) yields to create a table having var1, var2…varN variables. The data types as well as sizes of these variables can be different but they must have an equal number of rows. A table assigns variable names as var1, var2 by default to the output table but if the variable names are defined or the inputs are workspace variables then the defined variable names are assigned to the output table.

T = table(___,’VariableNames’,varNames) yields to create a table having variable names and variables values as arguments within the function.

T = table(___,’RowNames’,rowNames) yields to create a table having variable names and row names as the arguments.

How to Use table() Function in MATLAB for Creating a Table?

You can create a table in MATLAB by following the given steps:

Step 1: Collect all data which you need to convert into tabular form.

Step 2: Assign the variable names to the data chunks.

Step 3: Use any of the mentioned above syntaxes to create the table.

Examples

Consider some examples to understand the functionality of the table() function in MATLAB.

Example 1

The given example has the data of the five fruit shops that represent the number of fruits left in the shops. The table() function is used to convert this data into a tabular form.

Orange = [9;3;12;5;20;24];

Banana = [27;8;4;19;20;22];

Watermelon = [19;36;74;27;19;7];

Strawberry = [6;36;18;30;29;32];

Shops = [ 'A';'B';'C';'D';'E';'F'];

T = table(Shops,Orange,Banana,Watermelon,Strawberry)

This example first stores the data into variables in the form of column vectors and then uses the table() function which accepts all the variable’s names as the arguments and creates a table as we require.

Example 2

Here is another example that creates a table of similar data in MATLAB, but using a different table syntax.

T = table(categorical({'A'; 'B'; 'C'; 'D'; 'E'; 'F'}),[9;3;12;5;20;24],...

[27;8;4;19;20;22],...

[19;36;74;27;19;7],...

[6; 36; 18; 30; 29; 32],...

'VariableNames',{'Shops','Orange','Banana','Watermelon','Strawberry'})

In this example, the table() function accepts all the variable values and variables names as the arguments and assigns each variable name to its corresponding value, and creates a table as we require.

Example 3

This example has the same data as the above example but uses a different approach to implement the table() function for creating a table in MATLAB.

Orange = [9;3;12;5;20;24];

Banana = [27;8;4;19;20;22];

Watermelon = [19;36;74;27;19; 7];

Strawberry = [6; 36; 18; 30; 29; 32];

Shops = { 'A'; 'B'; 'C'; 'D'; 'E'; 'F'};

T = table(Orange,Banana,Watermelon,Strawberry,'RowNames',Shops)

The above example first stores the data into variables in the form of column vectors and then use the table() function which accepts all the variable’s names and the row name as the arguments and creates a table as we require.

Note that the Shops array is replaced with RowNames.

Conclusion


The table() function in MATLAB is a powerful tool for creating structured tables by organizing data in a column-oriented or tabular form. Each piece of data is treated as a variable, allowing flexibility in handling different sizes and data types. But, it is compulsory to ensure that every column has an equal number of elements. This article has provided syntax examples and implementation details of the table() function in MATLAB, demonstrating its usefulness in creating tables from various data sources.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.