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.

%% READING AN EXCEL SPREADSHEET IN MATLAB
% 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
clc
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(‘http://nm.mathforcollege.com’)
disp(‘  ‘)

%% INPUTS
% We have two column data and it has headers in the first row.
% That is why we read the data from A2 to B32.
A=xlsread(‘c:\users\grades.xls’,’A2:B32′);
disp (‘The data read from the excel spreadsheet is’)
disp(A)
disp(‘  ‘)
%% SOLUTION
% Finding the number of rows and columns
sizem=size(A);
rows_A=sizem(1);
cols_A=sizem(2);
% Assigning the scores to a vector called score
for i=1:1:rows_A
    score(i)=A(i,2);
end
% Using the max command to find the maximum score
% HW: Write your own function “max”
maxscore=max(score);
% Finding which student got the highest score
for i=1:1:rows_A
   if score(i)==maxscore
       student_no=i;
       break;
   end
 % HW: What if more than one student scored the highest grade??
end
%% OUTPUT
disp(‘  ‘)
disp (‘OUTPUT’)
fprintf(‘Student Number# %g scored the maximum score of %g’,…
    student_no,maxscore)
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.

0 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. 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…

  3. 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…

  4. 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.

  5. 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.

  6. 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!

  7. 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