Matlab

How to Create a Table Array with Named Variables Containing Different Types in MATLAB

Table arrays with named variables containing different types are powerful data structures in MATLAB that allow you to arrange and work with diverse data in an organized way. Picture a table where each column represents a unique type of information, and each row contains the specific values for those variables. These tables can hold a wide range of data types, including numbers, words, dates, and more. The beauty of table arrays is that they provide a flexible structure to store and manipulate such mixed data efficiently.

This article is going to demonstrate an easy way to create a table array having named variables containing different data types.

Create a Table Array with Named Variables Containing Different Types

To create a table array with named variables and different data types in MATLAB, you can use the table() function in MATLAB. This function takes one or more vectors or sets as the arguments and returns a table having size depending on the number of arguments.

The syntax for the table() function in MATLAB is given as:

T = table(var1,...,varN)

 
Here:

T = table(var1,…,varN) yields to create a table having var1, var2…varN variables. These variables can have different data types and sizes but 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.

Example

In this MATLAB code, we are going to create a table having the result data of the five students.

Name = {'Rivest';'Shamir';'Adleman';'Thomas';'Stewart'};
Marks = [900;1070;875;987;750];
Percentage = [82; 97; 80; 90; 68];
Grade = {'A';'A+';'A';'A+';'B'};
T = table(Name,Marks,Percentage,Grade)

 
In the above code, we created a table that consists of four columns that have different data types such as “Name” contains strings while “Marks” contains integer values. The created 5-by-4 table is displayed on the MATLAB output window.


The table variables can be accessed by using dot indexing. Like, if we require to compute the mean of all percentage values we will use T.Percentage to access the Percentage variable and then will calculate the average using the mean() function.

avg_percentage = mean(T.Percentage)

 

We can also use the dot index for adding a new variable to the table. In our previous example, we are going to add a new variable named Reg_Number that contains the registration numbers of all students.

T.Reg_Number = [26; 32; 57; 45; 23]

 

For further information on how to access table elements, you may visit the link.

Conclusion

Table arrays in MATLAB are like organized spreadsheets that allow you to store different kinds of information. They are flexible and allow you to have columns with names, each containing different kinds of data like numbers or words. Using the table() function, you can easily create these tables in MATLAB and work with your data in a structured and convenient way.

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.