13.3: Reading from an Excel file in MATLAB and Octave
- Page ID
By Carey A. Smith
MATLAB can read data from Excel files using the xlsread() command.
It helps if your are familiar with Excel's addressing of cells by columns (letters) and rows (numbers).
The xlswrite() command can be used to write to an excel file.
The attached Excel file, NBA_stats2016raw.xls, has statistics for the 30 NBA basketball teams.
This Matlab program reads this file and processes some of the data:
clear all; close all; clc; format compact
% Read the .xls file of NBA statistics
[nums,chars,raw] = xlsread('NBA_stats2016raw.xlsx');
% 3 matricies are returned.
% nums is a 31x26 matrix of just the numerical data. Since the first row has column headers, it is not part of the nums matrix.
% chars is a matrix of just the character data.
% raw is a matrix all of the data
% Extract the column of points for the 30 teams.
points_tot = nums(1:30, 25);
% Column 25 has the total points in the season
% The last row (row 31) has averages of the data,
% so it is not extracted.
% Compute the min, median, and max values of points_tot
points_min = min(points_tot)
points_median = median(points_tot)
points_max = max(points_tot)
Add example text here.
MATLAB can also write to files in the Excel format using the xlsopen() and xlswrite() functions. However, that is not discussed here.
The xlsread function is not part of the default Octave in my current version: GNU Octave Version: 7.1.0. It is planned to become part of the default Octave. Until it becomes part of the default package, you need to download and install the Octave io package from this link:
In needs to be installed into the GNU Octave program folder. It may expect to have Excel installed.
The download file is a zipped file with a .gz extension. This can be opened with various zip applications, including 7-zip, which is a free application.
This is a link has detailed instructions on how to use the Octave input/output (i/o) package:
https://wiki.octave.org/IO_package (Links to an external site.)
The basic functions--xlsopen(), xlsread(), xlswrite()--work the same in both Octave and Matlab. These are used in scripts.
Both Octave and Matlab also have interactive I/O functions.
If you have trouble using Octave's xlsread program, talk to your instructor.