If you are a regular MATLAB user, extracting data from a CSV file is an essential skill that should not be taken for granted. Since the data in the CSV file are in the form of a table, therefore extracting the data in the table form will help you analyze the data correctly. MATLAB provides the built-in readtable() function that makes the data-extracting task simple.
Read this guide for a detailed overview of how to extract data from a CSV file to a table in MATLAB using the readtable() function.
How to Extract Data from CSV File to Table in MATLAB Using readtable() Function?
The readtable() is a built-in function in MATLAB that is used for extracting data from the given CSV file. This function accepts the file name as a mandatory argument and displays the data extracted from the file in the form of a table on the command window.
Syntax
The readtable() function uses different types of syntaxes in MATLAB, which is given below:
T = readtable(filename,opts)
T= readtable(filename, name, value)
Here,
The function T = readtable(filename) creates a table containing data extracted from the file. That file could be a text file with a .txt extension, a spreadsheet file with a .xlsx extension, or a CSV file with a .csv file extension.
The function T = readtable(filename, opts) creates a table containing data extracted from the given CSV file by using the import options.
Example
Consider some simple examples that help you understand the implementation of the readtable() function to extract data from the CSV file in MATLAB.
Example 1: How to Extract Data from a CSV File to a Table in MATLAB Using readtable() Function?
The following example extracts the data from the CSV file using the readtable() function and displays it in the form of a table on the MATLAB command window.
Example 2: How to Extract Data from a Specific Column of a CSV File in MATLAB Using readtable() Function?
If you are interested in extracting the data from the CSV file, such as getting the information of a specified column, you can use the dot operator (.) for data extraction. This operator will help you extract the data of a CSV file in MATLAB. However, before that, you should load the CSV file in your MATLAB using the readtable() function. The example can be followed to extract data from a specified column of a CSV file in MATLAB.
Conclusion
CSV files are important files that store information about large datasets and can be helpful in MATLAB for performing machine-learning-related tasks. MATLAB provides several built-in functions for reading the CSV file, one such function is the readtable() function that is used for extracting data from a CSV file to a table. This guide has covered the basics of the readtable() function with its syntaxes and provides simple examples that will help you in extracting the data from a CSV file quickly.