MATLAB - Using XLSREAD to Import Excel Data
22 Apr 2009 Quan Quach 13 comments 4,826 views
Microsoft Excel is a common data format, so it’s a good idea to learn how to work with it in the MATLAB environment. There are a couple of ways to import Excel data, and we’ll discuss how to use the XLSREAD command to do it.
Contents
- XLSREAD - When Data Contains Numbers
- XLSREAD - When Data Contains Header Lines, Column Headers, and Numbers
- XLSREAD - When Data Contains Both Numbers and Strings
- Other Options for XLSREAD
- Next Time: XLSWRITE
XLSREAD - When Data is Numerical
When the Excel data consists entirely of numbers, importing the data to MATLAB is straightforward. For example, take the Excel file, test.xls, shown below:

We can import the data into MATLAB using the following command:
%import the excel data into MATLAB [numericalData] = xlsread('test.xls');
numericalData =
1.0000 50.0000 66.1000
2.0000 100.0000 32.4000
3.0000 150.0000 70.5000
4.0000 200.0000 33.8000
5.0000 250.0000 14.0000
6.0000 300.0000 187.7000
7.0000 350.0000 105.4000
8.0000 400.0000 88.1000
9.0000 450.0000 102.6000
10.0000 500.0000 64.6000
XLSREAD - When Data Contains Header Lines, Column Headers, and Numbers
Most of the time, you will probably be dealing with Excel data that contains header lines and column headers, as shown below. In this instance, it’s going to take a little more work to get the data that you want.

%import the excel data into MATLAB [numericalData, textData] = xlsread('test.xls');
numericalData =
1.0000 50.0000 66.1000
2.0000 100.0000 32.4000
3.0000 150.0000 70.5000
4.0000 200.0000 33.8000
5.0000 250.0000 14.0000
6.0000 300.0000 187.7000
7.0000 350.0000 105.4000
8.0000 400.0000 88.1000
9.0000 450.0000 102.6000
10.0000 500.0000 64.6000
textData =
[1x46 char] '' ''
[1x49 char] '' ''
'' '' ''
'Sample' 'Gen' 'Power'
%use the following code to verify the contents of the cell array
textData{1:2,1}
Sure enough, it matches!
ans = This data set was created on April 10th, 2009. ans = Blinkdagger.com owns all rights to this data set.
XLSREAD - When Data Contains Both Numbers and Strings
What happens when your data consists of both numbers and strings, as shown in the example below? When you have a mixture of data types, the best way to import the Excel data is in the raw data format. In this format, the data is stored into a cell array.

%xlsread allows you to store the raw data into a cell array [numericalData, textData, rawData] = xlsread('test.xls');
rawData =
[1x46 char] [NaN] [ NaN] [ NaN]
[1x49 char] [NaN] [ NaN] [ NaN]
[ NaN] [NaN] [ NaN] [ NaN]
'Sample' 'Gen' 'Power' 'Color'
[ 1] [ 50] [ 66.1000] 'Green'
[ 2] [100] [ 32.4000] 'Red'
[ 3] [150] [ 70.5000] 'Blue'
[ 4] [200] [ 33.8000] 'Green'
[ 5] [250] [ 14] 'Green'
[ 6] [300] [187.7000] 'Blue'
[ 7] [350] [105.4000] 'Red'
[ 8] [400] [ 88.1000] 'Red'
[ 9] [450] [102.6000] 'Black'
[ 10] [500] [ 64.6000] 'Yellow'
At this point, it will take a lot of cell array manipulation to get the data into the format that you want. ass=”comment”>%xlsread allows you to store the raw data into a cell array
[numericalData, textData, rawData] = xlsread(‘test.xls’);
rawData =
[1x46 char] [NaN] [ NaN] [ NaN]
[1x49 char] [NaN] [ NaN] [ NaN]
[ NaN] [NaN] [ NaN] [ NaN]
'Sample' 'Gen' 'Power' 'Color'
[ 1] [ 50] [ 66.1000] 'Green'
[ 2] [100] [ 32.4000] 'Red'
[ 3] [150] [ 70.5000] 'Blue'
[ 4] [200] [ 33.8000] 'Green'
[ 5] [250] [ 14] 'Green'
[ 6] [300] [187.7000] 'Blue'
[ 7] [350] [105.4000] 'Red'
[ 8] [400] [ 88.1000] 'Red'
[ 9] [450] [102.6000] 'Black'
[ 10] [500] [ 64.6000] 'Yellow'
At this point, it will take a lot of cell array manipulation to get the data into the format that you want. Read on for some more tips on importing data!
Other Options for XLSREAD
- Specify WorkSheet - Let’s say you have the following Excel file. Notice that the data is on Sheet 2 now.

By default, XLSREAD reads the first sheet within the Excel File. You can specify which sheet you want to read the data from by doing the following:
%specify which sheet you want to import data from [numericalData] = xlsread('test.xls','Sheet2');
- Specify Range - Let’s say you have the following Excel file, and you only want to extract the information in the red rectangle.

You can do this by using the following code:
%specify the sheet and the range you want to import [numericalData] = xlsread('test.xls','Sheet2','A9:C14');
- Cut and paste directly into MATLAB: This one doesn’t have anything to do with XLSREAD. It’s a technique that I use sometimes when I need to import data quickly. First, I create an empty variable, and then open up the array editor.
%create an empty variable data = []; %opens up the data variable in the Variable Editor openvar data;
Alternatively, you can open up the Variable Editor by double clicking on the data variable from within the main workspace.
Finally, paste the data into the empty array.
Next Time: XLSWRITE
Next time, we will discuss the intricacies of XLSWRITE.
13 Responses to “MATLAB - Using XLSREAD to Import Excel Data”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Huh. Never noticed the raw option before. That would have been handy a couple of times I that I have used XLSREAD. Thanks for the info.
Hey Quan,
Glad to see you covering XLSread / write. I’ve never been able to use XLSread any faster or more consistently than the copy/paste method. If it’s one-off data, I just copy/paste. If there are many spreadsheets, it seems inevitable that they do not all conform to the expected format (and I must revert to manual). This function is very sensitive to format.
XLSwrite, on the other hand, is one of my favorites. I use it quite often at the end of scripts to distribute my results to people who don’t have MATLAB. It also can be used to “archive” results that I run frequently, by adding a new tab onto the workbook each time. It’s easier than coming up with naming conventions for a bunch of .mat files.
Nice articles!
Rob
Hi Guys
This is gold-dust! A colleague and I were recently running a MATLAB training course and we were asked about how to read/write from spreadsheets by a lot of people. I promised to write an article explaining how to do it with the intention of distributing it to anyone who was interested.
Not only have you saved me a job but you have taught me a couple of tricks I didn’t know. Thank you! There will be much linking to this site
Best Wishes,
Mike
@Robert - The raw feature is pretty useful indeed! A lot of files I work with are a mixture of strings and numbers, so it’s good that this options exsits.
@RobS - XLSWRITE is pretty nifty! We’re going to be doing a tutorial on how to use that as well. Maybe you will have some tips to share with us?
@Mike - Thanks for the comment Mike! Stay tuned for XLSWRITE. I hope it’s platinum-dust this time around.
Great post as always, Quan. There’s one more really easy way to get mixed data in from Excel that I wanted to mention. Users of the Statistics Toolbox can use a dataset array (great example here), which stores columns of data that can be of mixed type (all rows of one column are of homogeneous type, but the columns can each be of heterogeneous types). I’ve found them to be a bunch more convenient than cell arrays for data I bring in from Excel. The best part is that they’ve built xlsread underneath the hood, allowing you to get data from Excel into a very convenient format in one shot.
Here’s a simple example, working with your example test.xls. I also make use of a related type called a nominal array, which allows us to express the color values as discrete values:
>> ds = dataset('xlsfile','test.xls','range','A4:D14','ReadVarNames',true) ds = Sample Gen Power Color 1 50 66.1 'Green' 2 100 32.4 'Red' 3 150 70.5 'Blue' 4 200 33.8 'Green' 5 250 14 'Green' 6 300 187.7 'Blue' 7 350 105.4 'Red' 8 400 88.1 'Red' 9 450 102.6 'Black' 10 500 64.6 'Yellow' >> % You can pull out raw data with dot notation >> ds.Color = nominal(ds.Color); >> ds ds = Sample Gen Power Color 1 50 66.1 Green 2 100 32.4 Red 3 150 70.5 Blue 4 200 33.8 Green 5 250 14 Green 6 300 187.7 Blue 7 350 105.4 Red 8 400 88.1 Red 9 450 102.6 Black 10 500 64.6 YellowHi Guys,
congratulations, nice articles!
Do you know, is it possible to test is a specified worksheet is exist or not, before use xlsread to avoid error message? I’m looking for something, what is more simple than work with the output of xlsfinfo.
Thanks!
Hi,
This is my first time posting on your website, but I’d like to say that your tutorials have helped me a lot with learning about matlab and all its functions.
I wanted to ask if you could write a tutorial about the dataset function. I just tried it out based on what Scott said, and it seems to work best for the dataset I am working with.
Hi,
This is my first time posting on your website, and I’d like to say that your tutorials have helped me in learning matlab .
I wanted to ask if you could write a tutorial about the dataset function.
If we are given a huge data containing thousands of rows and columns, with mixture of all the varieties,how to implement all the possible manipulations in the easiest way.Ex:take the tansactions in the bank of different depositers.
Hi 2 everyone,
i am new to matlab and trying to import data from excel sheet using following command as told by an experienced friend but repeatedly finding ‘Error using xlsread’ and ‘ cannot find the file’
A = xlsread(’c:\mydocuments\INPUT.xls’)
Plz help me to resolve this issue.
Thanking in anticipation
Hey…I am trying to import one of excel file with 17575 rows and 50 columns,using dataset but not able to….showing some error…please help me out !!!
tankssssssssssssssssssssssssssss
thanks that helped
hi,
can you plz tell me how can i convert an excel file into a matrix in matlab if column a in file should be the rows and the column b should be the columns name and the column c in the file should be the cells that we have to fill in the matrix.