Categories
1. Some Basic Tips

Matlab Table

In Matlab ‘Table,’ function is used to create the table. A table can contain different type’s data or information such as variables, values, constants, etc. It also has different sizes as long as all variables. The table function arranges the data into rows and columns as we define.

Syntax:

  • T = table(var1,…,varN)
  • T = table(___,’VariableNames’,varNames)
  • T = table(___,’RowNames’,rowNames)

How to Create a Table in Matlab?

The Table function is used for creating the table. A table can contain different types’ data and sizes.  So we need to take that all data and then create a table with different sizes and different data types.  To create a table the following steps are used.

Step 1: Read all the data from the file.

Step 2: Assign all data to a variable.

Step 3: Then use the appropriate syntax of the ‘Matlab Table’ function to create a table.

Step 4: Then execute the code.

Examples of Matlab Table

Here are the following examples mention below

Example 1:

Let us consider an example of a Table. In this example, we can see how to create a table using the Table function. We have data on the sale of fruits in five shops. We can create a table by using the Table function. First, we take all sell entries as per fruit name and then we make a table using the Table function. A variable names is taken are Mango, coconut, Banana, and Apple. [8; 4; 3; 10; 9] this data is assigned to the variable Mango. A variable coconut contain [8; 4; 3; 10; 9] data.  [11; 16; 14; 17; 14] that data is assign to variable Banana.  A variable Apple contain [76; 63; 31; 33; 19] data. All this data is the number of fruit boxes sold in 5 shops.  All appropriate data is loaded into the table and with the help of the Table function, a table will be created. The table function arranges all data into rows and columns as we define. Execute the code to create a data

Code:

clc;
clear all;
close all;
Mango = [8;4;3;10;9];
coconut = [8;4;3;10;9];
Banana = [11;16;14;17;14];
Apple = [76; 63; 31; 33; 19];
Shops  = [ 'X'; 'Y'  ; 'F'; 'J'; 'E'; ];
T = table(Shops, Mango, coconut, Banana, Apple)

Output:

Command Window:

Matlab table output 1

Figure 1

After executing the code, we see that a table is created in the command window.

The created table is shown in figure 1.

Example 2:

Let us consider one more example of Table. We have data on certain company’s products and their transport places. And as we know that for creating a table in Matlab we used the Table function. In this example, we can see another type to create a table. In this example, we can assign all the data when we use the ‘Matlab table’ function. We use the table function with proper syntax to create a table. “T = table ( categorical ( {‘D’;’C’;’B’} ), [16;32;8], …{ ‘XY’;’YZ’;’AB’ }, … VariableNames’, {‘Product_Name’,’Quantity’,’State’})” this line is created a table using provide data and variables. Here used variable Names are ‘Product Name’, ‘Quantity’, and ‘State’. The table function arranges all data into rows and columns as we define. After executing the code table will be created.

Code:

clc;
clear all;
close all;
T = table(categorical({'D';'C';'B'}),[16;32;8],...
{'XY';'YZ';'AB'},...
'VariableNames',{'Product_Name','Quantity','State'})

Output:

Command Window:

In the first example, we take all the data first, and then we use the Table function.

Matlab table output 2

Figure 2

Example 3:

Let us see one more example of the table. In this example, we see how to specify row names while creating a table in Matlab. As we know the in Matlab ‘table’ function is used for creating the table. So we use the table function to specify row names while creating a table in Matlab. So we first take all the data into variables. We have some data related to persons like their age, height, weight. So first we take all that data into variables. Age, Weight, Height, and Name these variables to contain all the data. Now we use the table function with proper syntax for creating a table. The table function arranges all data into rows and columns as we define. After executing the code table will be created.

Code:

clc;
clear all;
close all;
Name = {'A';'B';'C';'D';'E'};
Age = [18;19;16;17;18];
Height = [71;69;64;67;64];
Weight = [55;63;56;49;59];
T = table(Age,Weight,Height,'RowNames',Name)

Output:

Command Window:

Matlab table output 3

Figure 3

Leave a Reply

Your email address will not be published. Required fields are marked *