Lesson finished
Unlock all 49 lessons of Matlab and Simulink BasicsFull course ₹40,000 · paid
Reading and Writing Files in MATLAB: fopen, xlsread, MAT Files
A free lesson from Skill-Lync's Matlab and Simulink Basics course, on Grow by Skill-Lync · Last updated 16 Nov 2026 · Session: “Reading Data from Files in MATLAB”
What will you learn in this free Matlab and Simulink Basics lesson?
Using a list of prime numbers between 1 and 50, the instructor reads text files with fopen, fscanf and fclose, reads Excel data with xlsread and comma-separated data with dlmread, and writes with fprintf. The lesson ends with saving and loading workspace variables in MAT files.
- Opening, reading and closing files with fopen
- Reading Excel data with xlsread
- Reading delimited data with dlmread
- Saving and loading variables as MAT files
What comes after lesson 3?
Later in Skill-Lync's Matlab and Simulink Basics course (paid): lesson 4, MATLAB Demo and Workspace Management; lesson 5, MATLAB and Simulink Basics; lesson 6, Programming and Graphics in MATLAB. The full course has 49 lessons (17 h 10 min of video); see the course overview or its price and syllabus.
Lesson transcript
Machine transcript of the lesson audio (lightly cleaned). Technical terms may contain errors; the video is the reference.
So continuing on with inputs and outputs, we said in previous section MATLAB has utilities to read data from files and write data to the files. We might have said this example was to show how we can write to a file, but I gather we are trying to read data from a file in this particular case. So we'll learn how the data can be read from a text file. The prime numbers between 1 and 50 were saved to a text file called prime underscore numbers dot txt as shown on the right.
It's a notepad output. I might have said Excel. That's not the case. It's the output from MATLAB session was saved to a notepad document. And so before we can read data from a text file, we have to define a file pointer. I mean, those who have programmed in C, they know file pointers. So here in MATLAB, what's shown on the right, the first line at the very top is defining a file pointer, FID, with an FOPEN command. The FOPEN takes two arguments. The first argument is the name of the file.
The prime underscore numbers dot txt file was already created with the prime number shown above the command window. And the second argument is an option. What we want to do with the file, read which mode we want to select. So in this case, we want to read the data from the file. So R is the read mode. And then once FID is defined, the file pointer, MATLAB supports FSCANF. So the FSCANF function needs the file pointer and the format that we want to read the data into.
The percent D is integer format. So it's going to read integer by integer every time. It looks like the entire file, all numbers in the file, will be read into prime numbers in integer format. When we type prime numbers in command window, it displays the numbers that were read into prime numbers variable using the FSCANF command. Basically, they're all displayed in column format. So data is read into the workspace using the file pointer on the FSCANF command with the percent D format specifier for the double data type.
Once the data is read, we have to close the file pointer. And that can be done with FCLOSE command. Now, so this example shows how data can be read from an Excel spreadsheet. I know previously I said we're trying to read data from an Excel spreadsheet, but that turned out to be a notepad document. But here we have an example that shows how data from an Excel spreadsheet can be read into MATLAB. So MATLAB has a function called XLSREAD. XLS for Excel, you know, how an XLS extension will be created for Excel spreadsheets when we create an Excel spreadsheet.
So XLSREAD is a function built in MATLAB to read data from Excel spreadsheet. XLSREAD will ignore all empty cells. And it will read the data or contents the cells are populated with. And then when XLSREAD is invoked, it will return the contents of all those cells into a variable. Here we call it matrix. I mean, we created a matrix in Excel and then we wanted to save it to a matrix. So we picked numbers from 1 to 9 to create that matrix. And when XLSREAD function is executed, the matrix, all 9, the data from all 9 cells has been read into matrix variable.
That's what we see on this slide. So now we'll take a look at another function called DLMREAD, where, I mean, DLM stands for delimited. So when we create data, we can create data with whitespace separation or comma separation or a tab separation. So what's shown on the right, again, in the notepad output, we have created the same set of prime numbers separated by commas. And we are going to exercise the DLMREAD on that file and see what the data looks like when the data is read into workspace using DLMREAD command.
So that's an example to show how data, comma-separated data, can be read into MATLAB. So the delimiters that are supported by MATLAB are space, comma, or tab, like I said before. The prime numbers that we have previously dealt with are used with each number separated by a comma, and it's shown on the right. And the DLMREAD is used to read the data back into MATLAB workspace. So that's what this exercise is all about. So when we create the variables in MATLAB workspace, we also want to save the data for later use.
I mean, we want to save the data, come back, you know, load the data. I mean, let's say somebody is interested in creating a huge data file with temperatures, you know, daytime temperatures across a year. You know, you have 365 days in a year. So that's a lengthy process, but, you know, in one session, one could create with 100 entries, and then they want to save the data and load the data and update the data. So, you know, MATLAB has the ability to facilitate that. So, in this case, we are trying to show how data created in MATLAB can be saved.
So, prime numbers is created as a row vector with all prime numbers between 1 and 50, and we create a file pointer, FID, but this time we open the file for writing purposes. So, that's why we chose W as the second argument for FOPEN. The prime numbers, I mean, here we are trying to write the values in the prime numbers vector to a file in a specific format, for example. So, we ought to know the length of the vector. So, we picked, we selected a variable LEN to represent the length of the prime numbers vector.
LEN, LEN, GTH is a function in MATLAB that returns the length of its input argument, in this case, prime numbers. And then we use a for loop to print the individual numbers to a file. So, we have a for loop with I being used as an index, going from 1 to the maximum length of the prime numbers vector we have on hand, which is LEN. And we use a function called fprintf. I mean, that is, printf and fprintf are commonly used in C language, and MATLAB just picked up the same names.
So, fprintf takes the file ID as the first argument, followed by the format specifier, and what you want to write. So, we are looping through the length of the prime numbers vector to print one number at a time. And then, once that is done, once you finish looping through, we close the file pointer with fclose command. And the output is written to prime numbers out.text file. So, we will see what the output looks like. So, you know, just an explanation for what the length command does, then how we loop through, how we close the file pointer.
So, what's shown on the right is the, it's opened in notepad, it's a text file, prime numbers out.text file with the prime numbers written to it. So, in this process, I mean, if you look at the command window on the right, and we run whose command, we know what variables got created. So, we created fid, file pointer. There is an index i, variable i, representing the looping counter. Len, we created a variable len that has the length of the vector, and the actual prime numbers. So, fprintf command takes file pointer as the first argument, and a format specifier as the second argument, and the actual variable that we want to print.
So, the same fprintf is rerun with a different format specifier shown on the right. Backward slash n is a new line character. If we use that as part of the format specifier, then it will add a new line after the value is printed. Let us see if the output is shown. We already have defined LEN. That's why we didn't have to redefine it. So we created, we are writing to a different file this time with a different format specifier. So prime numbers out underscore to dot text. Let's see what the output looks like.
So the notepad output, or the output text file in notepad, has all prime number entries written on new lines. So if somebody wanted a different format, that format specification should go into the fprintf command. So, so far we have covered reading from files and writing to files. So let's move on to loading and saving data. So anytime we create data, we want to save it to your file for later use. And then we want to load that file back into MATLAB workspace and keep working on it and save it again or perform an operation that you would like to do.
So MATLAB allows saving workspace variables in multiple ways as shown on the right. There is a command called save command and save takes file name and it also takes the variable names. So the first one, the first argument for save is the file name and the second argument could be a list of variables that you want to save. We have to make sure that those variables are in the MATLAB workspace. I mean, if we don't specify anything, it will work, it will save everything in the workspace to that file.
On the other hand, if we specify a variable, it will only save that variable to the, to the file. So that's what we are saying. Save file name comma variable saves the variable specified to your binary file. And, and the dash append cloud argument will save additional variables to an already existing MAT file. The .mat is a data file, binary data file. The extension .mat indicates that it's a binary data file. So once the, the workspace is saved, all variables are saved, one could go ahead and clear the workspace, start a different task.
So clear all is one such command to clear the workspace. Now, taking a look at how workspace variables can be saved and we have cleared the workspace. Now let us see how we can retrieve the variables back into, back into the workspace. So for doing that, we use a command called load. So the load command reads a binary .mat file and extracts all variables and saves them to the workspace or loads them into the workspace. So in this case, we are trying to load var1.mat file.
When we loaded, it had variable 1, var1, which took 88 bytes of, then when var2.mat was loaded, there was an additional variable. I believe we saved var1 and var2 to var2.mat file, as shown on the right. So that's what we have. Load, file name variables, loads, only those variables specified into the workspace. Also, we can be selective about what variables we want to load. Let us say we have created a workspace.mat file with a bunch of variables, but we only want to load one variable called var1, then you should be able to do that by passing var1 as a second argument.
So the examples are shown on the right. I think when the demo is given, I think, you know, the things will become a lot clearer. Right now, we are only showing or looking at command window. There are other MATLAB windows which show the workspace variables. I mean, here we have to enter who to see what's in the workspace, but MATLAB has a utility, you know, has a window where the workspace is continuously displayed. So here we'll stop for MATLAB demonstration, which would be in the next section.