January 3

MATLAB Read CSV: Everything to Know

MATLAB Read CSV: Everything to Know

Tandose Sambo

Table of Contents (click to navigate)

Introduction to CSV files

In a very interesting fashion, there is a point where there’s a need to fuse a little computer science theory with the MATLAB system. MATLAB, as an open structure, has the ability to integrate with various file types as inputs, or to generate the required type of files in the format that is needed, after creating the calculation or simulation from scratch. 

CSV files, also known as comma-separated-values are text-based files that contain data, which can be processed via various computer software systems such as Microsoft Excel, Microsoft Word, as well as the equivalent on Apple systems or on open source software such as Google Docs or Linux Based systems.

The types of data stored by the file are both numerical and text based, with each file line collated by the data system in the format of a record. Other sub-categories of data storage are acknowledged by the system, but for the purposes of this tutorial we will not delve into the deeper Computer Science details. As a general rule of computing, the CSV format is identified by the fact that the data collated are separated by commas as a means of organizing the data in a desired sequence. 

How do you create a CSV File? 

CSV files can be created via a myriad of options. These include a direct creation in a text based file such as Notepad. In spreadsheets and word documents, once files are created, one has the option to export the file as a CSV file. 

Let us use a simple example with a data set for some swimmers, and their freestyle times. 

Name of SwimmerAge School AttendedTime/secs
Jane Stephenson18Campion High School32
Catherine Crawford17St James Technical28
Faye Robertson18Montego Bay High School31
Michael Bernard17Vere Comprehensive High27

Based on the discussion that was held above, the relevant data for the students outlined as a CSV flow would be collated as follows. The system collates all the data, and separates them by commas. The language is more lower level, but still able to be read by the computer systems as distinct variables. For the first row in the data set, it will best be represented by: 

Jane Stephenson, 18, Campion High School, 32

The subsequent rows of data would be represented in a similar manner and organized in the same sequence as outlined in the image below.

The relevant fields of data are organized in rows, with the rows themselves being organized by individual lines. Once the files are created, they can be saved in the relevant format. In Notepad, simply enter File 🡪 Save As, and the system will automatically save the file as a .txt file. This file can subsequently be imported into a medium like Excel and converted. If the data is entered directly into Excel, then the option to directly convert the data to a .csv file will be available once the data is saved. With that information in our arsenal, let us now delve into the integration of the .csv file into the MATLAB system.

What is MATLAB Read CSV?

MATLAB, just like Microsoft Excel and other text-based software, has the ability to read .csv files, once they have been created by the user. The MATLAB system, as a syntax-based and code-based system, has the ability to read the files once the appropriate instructions have been given. Occasionally, MATLAB has overlapping syntax options so while CSV read is an option available to the user, the MATLAB system has instructed the user base to opt for the syntax read matrix instead. An interesting twist in the flow also leads us to the MATLAB ribbon instead of using the code. As the systems in the MATLAB options are evolved, some codes are phased out. From MATLAB’s system, the following is the main option that is available to the user for CSV import. 

M = csvread(filename)

Depending on your data needs, the appropriate option will subsequently be chosen. MATLAB has more advanced options, but for the purposes of this tutorial, we will use the simplest one.  The first syntax is used for files that contain only numerical values, while the latter two options enable data to be selectively chosen from a sequence based on pre-identified criteria. The key to theory application is practice. Let us now delve into one option that we can utilize, in order to ensure that the skillsets are engrained in our brains! Let’s jump in. 

Brief Tutorial: MATLAB Read CSV 

Let’s put the power of MATLAB to use. In this initial example, the first thing we are going to do…is start with a fresh command page. MATLAB, as a license-based system enables the user to log in using either an industry based, or educational system based login. Once you have logged in, the following should be your screen: 

With a fresh command page open, we will go ahead and enter data from a pre-generated .csv file. The file data is highlighted as follows: 

With MATLAB, the user has the option to read the data, as well as carry out a few calculations pertaining to the data that was collated. The following is the instruction that will be given to MATLAB. In Excel the file name is Swimmer’s Data 50 M

Freestyle, but in the process of saving as a .csv file, the file was simply called “swimmerdata”. The MATLAB syntax is highlighted below: 

filename = “swimmerdata.csv”

M= csvread(filename) 

Let us enter this data into the MATLAB system and see what the system produces. The screen image is highlighted below: 

In the MATLAB 2019 environment, using the syntax given, the system actually generates an error message. Based on what was indicated in the preamble under the definition of the code, there is an alternate path that we will have to take in order to achieve the desired objective. While alternate code is a possibility, the ribbon helps us to achieve the same objective. As with all things in life, the road may not always be a straight line, but you get there eventually. Before you start worrying, let us now explore the section of the command window known as the ribbon. Thankfully, MATLAB as a system has the option for the user to manipulate data, or to import or export it. 

In most of the activities we have done, the exercise tends to focus on straight code entry. In this instance, we can attempt to use some of the alternative built in paths, and see what the system will enable us to do. Looking from the left to the right you see that there are various options for you to tap into as users. The first option is for you to enter a new script, or enter a new live script. This segment will be helpful in the future. Scrolling to the right, there is an Import Data tab. Go ahead and press this button. The following is what will appear on your screen. 

Once the file has been identified by MATLAB, as a user, you will press the Open button, as highlighted in the screen capture. The following subsequent screen will appear. MATLAB pre-reads the data, so what your system will indicate is a feel for the information that will entered into the system before you actually import it. This is a preview of the coming attractions. 

With the data previewed, as a user, you then press the Import Data button, as highlighted to the top right-hand corner of the screen. 

The final image above is the final result! Although a longer route, the final outcome is the desired effect. Once the Import Data button is pressed, a small confirmatory message will be generated by the system. Acknowledging the message will return you to the Command window, which will initially have an empty window. The workspace, to the bottom left-hand side of the screen, will have an indication of the data table.

Once you double click on swimmerdata, the text will be highlighted as indicated on the screen, and your data will populate its own window just above the Command window. With the data now available, as a user you are open to the various possibilities of data calculations that you’ll need to convert your data into information. Congratulations, you have learned how to create a .csv file, and enter the data into the MATLAB system. 

MATLAB as a system, has the option for users to sort data, and utilize other mathematical functions in order to ensure that the data is presentable in formats such as graphs or other visual media. 


The CSV file is a useful format in Computer Science. As a means via which data can be collated into rows and columns and sequenced via commas, the format is a very compact means via which the user can organize data, and export it to a myriad of systems including open source and proprietary data. MATLAB as a system is open to the use of the CSV file, and recognizes it as an import standard. With the ability to use either code or pre-built import options, the user will have the ability to utilize the relevant options that suit their convenience in order to achieve the final results.

Loved this? Spread the word

About the Author

Tandose Sambo is a Chemical Process Engineer, with a focus on improving process efficiency via operational improvements. Six-Sigma certified, and with a Design-focus and Data Analytics interest, she is a driven growing entrepreneur, with the intention to optimize industrial and business process operations. Her interests include sharing time with family and travelling.

Tandose Sambo

Related posts

Compare Autodesk Inventor vs Fusion 360: Review Which One to Buy

​Read More

How to Use Convert Entities Tool in SolidWorks: Beginner Tutorial

​Read More

[May 2020] Best Free CAD Design Software for Mac

​Read More

La mejor computadora portátil para SOLIDWORKS en 2019: precio, revisión, especificaciones recomendadas

​Read More
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Subscribe to our newsletter now!