EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab readtable
 

Matlab readtable

Updated March 13, 2023

Matlab readtable

 

 

Introduction to Matlab readtable

Read table function in MATLAB is used to create a new table by reading data present in the form of columns in a file. The file from where the read table function can read the data can be a text file, a comma-separated or csv file, or some other excel workbook. The functionality of creating a table by reading from a text file can also be achieved in MATLAB using the in-built Import tool. Please note that we will be utilizing in-built MATLAB files for this article. You can use the same code to create tables from files stored in your local machine. In this topic, we are going to learn about Matlab readtable.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax of read table function:

R = readtable (file)

R = readtable (file, x, y)

Description of the syntax:

  1. R = readtable (file) is used to create a table in MATLAB by reading the data in the file called ‘file’. Here ‘file’ can be a text, csv, or any other excel file with column-oriented fields.
  2. R = readtable (file, x, y) is used to create a table in MATLAB by reading the data in the file called ‘file’. Here the extra arguments passed are to control the type of table we create from the input data file. We can control parameters like formats of the columns, number of rows or columns we need

Examples of Matlab readtable

Let us now understand the code to create a user-defined function in MATLAB

Example #1

In this example, we will create a table in MATLAB by reading the data from an in-built text file using the readtable function. Below are the steps to be followed:

  1. Initialize the function readtable with the file to be read as an input argument
  2. For this example, we will use the in-built text file called ‘grades.txt.’

Code:

R = readtable (‘grades.txt’)
[Initializing the readtable function and passing the in-built file ‘grades.txt’ as the argument] [readtable function will read this text file and will give a table as an output]

This is how our input and output will look like in MATLAB:

Input:

Matlab readtable output 1.1

Output:

Matlab readtable output 1

As we can see in the output, the readtable function has read the grades.txt text file and given us a table as the output.

In the above example, the text file has only 4 rows and 4 columns; our output table is very small. However, there might be cases where we have a very large file, and we need to view only a small portion of it as a table.

Example #2

In this example, we will create a table in MATLAB by reading the data from an in-built text file using the readtable function. The file we will be using in this example has 123523 rows and 29 columns. Therefore, we will first create a table for this entire file, and in the next example, we will see how to get a sub-set of this table. Below are the steps to be followed:

  1. Initialize the function readtable with the file to be read as an input argument
  2. For this example, we will use the in-built csv file called ‘airlinesmall.csv

Code:

R = readtable (‘airlinesmall.csv)
[Initializing the readtable function and passing the in-built file ‘airlinesmall.csv’ as the argument] [readtable function will read this csv file and will give a table as an output]

This is how our input and output will look like in MATLAB:

Input:

Matlab readtable output 2.1

Output:

output 2

As we can see in the output, the readtable function has read the airlinesmall.csv file and given us a table as the output. Since the output table is too large to fit in one screen, we have the scroll option in the output.

Next, we will learn how to get a sub-set of this table as the output.

Example #3

In this example, we will use the same ‘airlinesmall.csv’ file to create a table in MATLAB but will create a sub-set of the entire table. As we noticed in the output in the previous example, the number of rows and columns in the ‘airlinesmall’ data were 123523 and 29, respectively. Therefore, we will now create a sub-table with 5 rows and 4 columns. Below are the steps to be followed:

  1. Initialize the function readtable with the file to be read as an input argument
  2. Here also we will use the in-built csv file called ‘airlinesmall.csv
  3. Pass the range which we need as the argument

Code:

R = readtable (‘airlinesmall.csv)
[Initializing the readtable function and passing the in-built file ‘airlinesmall.csv’ as the argument] R (1:5, 1:4)
[Passing the range, we need as argument]

This is how our input and output will look like in MATLAB:

Input:

output 3.1

Output:

output 3

As we can see in the output, the readtable function with the range passed as an argument has read the airlinesmall.csv file and given us a sub-table as the output.

Conclusion

  1. By using the readtable function, we can create a table in MATLAB by reading data from a file.
  2. The different file extensions like .txt, .csv can be passed as an argument to the readtable function
  3. We can use the readtbale function to get a sub-set of the entire table if required.

Recommended Articles

This is a guide to Matlab readable. Here we discuss the Examples of Matlab readable along with the inputs and outputs. You may also have a look at the following articles to learn more –

  1. Matlab Backslash
  2. Plot graph Matlab
  3. Matlab Saveas
  4. Impulse Response Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW