Matlab

How to Read a Table in MATLAB?

In MATLAB, tables are the most effective way to display spreadsheet data since they can hold various data types, including text, numbers, variable names, row, and column names. We can read data from a table using the readtable() function that allows us to read data into tables programmatically.

Follow this guide to learn how to read a tale in MATLAB using the readtable() function.

How to Read a Table in MATLAB?

The readtable() function is a built-in function in MATLAB that is used to read tabular data from various file formats and create a table object in MATLAB. It is commonly used to import data from files such as CSV (comma-separated values), XLSX (Excel), TXT (text), and more.

The readtable() function accepts the file name or file path as input and provides a table object holding the data from the file. The table object represents the data in a structured format with rows and columns, similar to a spreadsheet or database table.

Syntax for readtable() Function in MATLAB

The readtable() function in MATLAB follows a simple syntax that is given below:

T = readtable ("file")
T = readtable ("file", Name, Value)

 
Here:

    • T = readtable (“file”): This syntax is used to make a table by reading the data from the “file”. In this case, “file” can be of any type including an Excel file, text file, CSV file, and others with column-oriented fields.
    • T = readtable (“file”, Name, Value): This syntax allows you to specify additional name-value pairs to customize the import process. The Name and Value pairs provide control over various options such as the number of rows or columns to read, column formats, variable names, handling missing values, and more.

Example 1

In this example, we read the entire data from the user-defined Excel file using the readtable() function and create a new table T to store the data read from the given file.

T= readtable("Komal [June].xlsx")

 

Example 2

This MATLAB code reads entire data from the built-in MATLAB CSV file using the readtable() function and creates a new table T to store the data read from the specified file.

T=readtable("airlinesmall.csv")

 

Example 3

Consider Example 2 to read specific data from the given built-in MATLAB CSV file using the readtable() function. In this example, the readtable() function just reads the first five rows and the columns from 2-9 from the given file by storing this data in the newly created table T.

T=readtable("airlinesmall.csv");
T(1:5, 2:9)

 

Conclusion

The readtable() function in MATLAB is a powerful tool for reading data from files and creating tables in a column-oriented format. By accepting files of any format as input, it enables the creation of tables that store the data extracted from the files. This tutorial provided practical examples to demonstrate how to read data from both user-defined and built-in MATLAB files, showcasing the usefulness of the readtable() function.

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.