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
disp(‘This program shows how to read an excel file in MATLAB’)
disp(‘Authors : Autar Kaw’)
disp(‘Last Revised : December 12, 2010’)
% 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’)
% Finding the number of rows and columns
% Assigning the scores to a vector called score
% Using the max command to find the maximum score
% HW: Write your own function “max”
% Finding which student got the highest score
% HW: What if more than one student scored the highest grade??
fprintf(‘Student Number# %g scored the maximum score of %g’,…
This post is brought to you by
- Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com,
- the textbook on Numerical Methods with Applications available from the lulu storefront,
- the textbook on Introduction to Programming Concepts Using MATLAB, and
- the YouTube video lectures available at http://nm.mathforcollege.com/videos
Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.
3 thoughts on “Reading an excel file in MATLAB”
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.
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.
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!