Skip to main content
Library homepage
 
Engineering LibreTexts

13.3: Reading from an Excel file in MATLAB and Octave

  • Page ID
    85196
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    By Carey A. Smith

    MATLAB

    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.

    Example \(\PageIndex{1}\) Read NBA 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:

    %% NBA_Excel_read.m
    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)

    Solution

    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.

    Octave

    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:

    https://octave.sourceforge.io/io/

    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.

    https://www.7-zip.org/

    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.


    This page titled 13.3: Reading from an Excel file in MATLAB and Octave is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.

    • Was this article helpful?