Reading an excel file in MATLAB

Recently I taught a volunteer class to professional engineers on MATLAB.  Two of the most requested items of interest were
1. How do I read an excel file?
2. How do I do curve fitting?

We address the first question here.  It is easy to read an excel file with the xlsread command but what do you with it once the file has been assigned.  So we took a simple example of an excel spreadsheet where the first column consists of a student number and the second column has the examination scores of the students.  You are asked to find the highest score.

The MATLAB program link is here.
The HTML version of the MATLAB program is here.
The Excel file used in the MATLAB program is here

It is better to download (right click and save target) the program as single quotes in the pasted version do not translate properly when pasted into a mfile editor of MATLAB or you can read the html version for clarity and sample output.

% Language : Matlab 2008a
% Authors : Autar Kaw
% Last Revised : December 12, 2010
% Abstract: This program shows you how to read an excel file in MATLAB
% The example has student numbers in first column and their score in the
% second column
clear all
disp(‘This program shows how to read an excel file in MATLAB’)
disp(‘Matlab 2008a’)
disp(‘Authors : Autar Kaw’)
disp(‘Last Revised : December 12, 2010’)
disp(‘  ‘)

% We have two column data and it has headers in the first row.
% That is why we read the data from A2 to B32.
disp (‘The data read from the excel spreadsheet is’)
disp(‘  ‘)
% Finding the number of rows and columns
% Assigning the scores to a vector called score
for i=1:1:rows_A
% Using the max command to find the maximum score
% HW: Write your own function “max”
% Finding which student got the highest score
for i=1:1:rows_A
   if score(i)==maxscore
 % HW: What if more than one student scored the highest grade??
disp(‘  ‘)
disp (‘OUTPUT’)
fprintf(‘Student Number# %g scored the maximum score of %g’,…
disp(‘ ‘)

This post is brought to you by

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.


Author: Autar Kaw

Autar Kaw ( is a Professor of Mechanical Engineering at the University of South Florida. He has been at USF since 1987, the same year in which he received his Ph. D. in Engineering Mechanics from Clemson University. He is a recipient of the 2012 U.S. Professor of the Year Award. With major funding from NSF, he is the principal and managing contributor in developing the multiple award-winning online open courseware for an undergraduate course in Numerical Methods. The OpenCourseWare ( annually receives 1,000,000+ page views, 1,000,000+ views of the YouTube audiovisual lectures, and 150,000+ page views at the NumericalMethodsGuy blog. His current research interests include engineering education research methods, adaptive learning, open courseware, massive open online courses, flipped classrooms, and learning strategies. He has written four textbooks and 80 refereed technical papers, and his opinion editorials have appeared in the St. Petersburg Times and Tampa Tribune.

3 thoughts on “Reading an excel file in MATLAB”

  1. Hi Mr.Autar Kaw
    thank you for your program,,,,
    I need your help to read data from excel file,
    I am working on Matlab R13 ,when I read the Data from excel file, the program Displays error message. I don’t know why?
    Please help me if you can.
    Note that : my excel file contains a large data, where each row in my excel file its length 10000.
    thank you…


  2. Dear
    may because of more data if you have data in more than one sheet so after writing the file name in quote you have to write the ‘sheet#’ also then specify the column # . try this hoping that you will got the solution.


  3. this program is for this particular excel file. so when you download the program, download the excel file as well.. save it in the same directory as the one opened in your matlab. then in line 20, enter the complete path name. you’ll be able to run it without errors.
    Hope this helps!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: