How To Import Data from .CSV File With Numeric Values and Texts Into MATLAB Workspace?
Last Updated :
28 Apr, 2025
A .csv file is a comma-separated file, in which consecutive data columns are separated by commas and the end of a line is the default EOL character. While dealing with small data, it is common to use a .csv file for storing it. In this article, we shall discuss how to import .csv files, with numeric data and their text headers as the column variables, into a MATLAB workspace.
Read tables in MATLAB
We can use the simple readable command to import the data from a .csv file as a table with column variables (headers). The readable command default detects the file type and makes the first row the column variables if it is in text form.
Syntax
tab = readtable("file_name.extension")
See the following example to understand the same.
Example 1:
Matlab
%MATLAB Code
tab = readtable("data.csv");
disp(tab.Properties.VariableNames)
In this example, we import a .csv file with numeric data and display its variable names (headers). The following .csv file is used.
Now, the above code will give us the following output:
Further, this data can be manipulated as per the user's requirements.
Import data in MATLAB
There is another way to the importing a .csv file with numeric data and text headers. The import data function of MATLAB allows performing the same functionality as the readable function however, the former creates a struct while the latter creates a table.
Syntax
tab = importdata(filename)
See the following example to understand the same.
Example 2:
Matlab
%MATLAB Code
tab = importdata("data.csv");
%displaying data
fprintf("Data\n")
disp(tab.data)
%displaying column headers
fprintf("Column headers\n")
disp(tab.colheaders)
This code will import the data as a struct with three fields:
Now, we can access these fields by struct methods. See below the output of the previous code.
Similar Reads
How to plot data from a text file using Matplotlib? Perquisites: Matplotlib, NumPy In this article, we will see how to load data files for Matplotlib. Matplotlib is a 2D Python library used for Date Visualization. We can plot different types of graphs using the same data like: Bar GraphLine GraphScatter GraphHistogram Graph and many. In this article,
3 min read
How to Retain Number Formats when Inserting Data into an XLSX Retaining the number formats when inserting data into an xlsx file in R language is a common issue faced by data analysts. Number formats include data types such as currency, percentage, date, and time. Losing these formats can lead to confusion and difficulty in understanding the data. However, wit
5 min read
How To Import Text File As A String In R IntroductionUsing text files is a common task in data analysis and manipulation. R Programming Language is a robust statistical programming language that offers several functions for effectively managing text files. Importing a text file's contents as a string is one such task. The purpose of this a
6 min read
How to Append Data to a File in MATLAB? Appending data to a text file means adding data to a file that already exists in the storage. print() function is used to write/append data to a file in MATLAB. It writes formatted text to a file based on the format string provided to it. The format of the output/input is determined by the formattin
4 min read
How to read a numerical data or file in Python with numpy? Prerequisites: Numpy NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. This article depicts how numeric data can be read from a file using Numpy. Numerical data can be present in different forma
4 min read
Write Data to Text Files in MATLAB Writing data to a text file means creating a file with data that will be saved on a computer's secondary memory such as a hard disk, CD-ROM, network drive, etc. fprintf() function is used to write data to a text file in MATLAB. It writes formatted text to a file exactly as specified. The different e
3 min read