Matlab

How to Add, Delete, and Rearrange Table Variables in MATLAB

Tables are powerful data structures in MATLAB that allow you to organize and manipulate data effectively. In addition to storing data, tables also provide flexibility in adding, deleting, and rearranging variables within them.

This article will guide you through the process of performing these operations on table variables in MATLAB, helping you manage and customize your data with ease.

1. How to Add Table Variables in MATLAB?

MATLAB allows us to add variables in the table:

1.1. How to Add Table Variables Using a Dot Operator?


We can add variables in the existing table using the dot operator. The newly added variable will be placed as the last variable and it must have an equal number of rows to the pre-existing variables. For example,

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);

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

The above code adds the “Reg_Number” variable to the table “T” with the specified values.

1.2. How to Add Table Variables Using addvars() Function?

The addvars() is a built-in function in MATLAB used for adding a new variable in the existing table. This function can add a new variable at any location before or after any existing variable in the table. For example,

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);

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

T = addvars(T,Reg_Number,'Before',"Marks")

The above code adds the “Reg_Number” variable before the “Marks” variable in the table “T” using the addvars() function.

2. How to Delete Table Variables in MATLAB?

We can delete any table variable in MATLAB.

2.1. Using removevars() Function

This removevars() is a built-in function in MATLAB that allows us to delete one or more variables from the given table. This function accepts the table name and variable names as inputs and returns a new table that does not contain the deleted elements. For example:

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);

T = removevars(T,["Grade","Marks"])

The above code removes the “Grade” and “Marks” variables from the table “T” using the removevars() function.

2.2. Using Dot Operator

This is an alternative method for deleting variables from the table in MATLAB. Using this method, we can delete a variable by mentioning the variable name after the dot operator and keeping it equal to empty square brackets. For example,

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);

T.Grade = []

The following example deletes the “Grade” variable from the table “T” by setting it equal to empty square brackets (T.Grade = []).

2.3. Using the Indexing Method

Another method for deleting variables from the table is indexing. This method works in the same way as matrix indexing. In this method, we use the colon operator to select the rows of the specified variable we need to delete. For example,

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);

T(:,"Percentage") = []

3. How to Rearrange Table Variables in MATLAB?

The table variables can be rearranged in MATLAB:

3.1. Using movevars() Function

The movevars() is a built-in function in MATLAB that is used for moving or rearranging table variables. This function accepts the table name, the variable name which we require to move, and a variable name before or after which we want to move the specified variable. For example:

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);

T = movevars(T,"Percentage",'After',"Grade")

3.2. Using the Indexing Method

This is another method used for rearranging the table variables. This method rearranges table variables according to the given row numbers. For example:

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);

T = T(:,[1 2 4 3])

Conclusion

MATLAB allows us to add, delete, and rearrange the table variables using various methods. To add one or more table variables, we use the (.) operator and addvars() function. To delete the table variable, we use the dot operator, removevars() function and indexing method. To rearrange the table variable, we use movevars() function and the indexing method. This guide taught us how to add, delete, and rearrange the table variables in MATLAB.

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.