Support Articles

Support Articles Technical Computing Interfacing MATLAB with Excel

Interfacing MATLAB with Excel

This article shows how data can be written from MATLAB to an Excel spreadsheet and how data can be read from and Excel spreadsheet into MATLAB. The ubiquitous nature of Excel makes it popular for data storage and a mechanism by which data can be easily shared in a technical environment. Getting this data into MATLAB allows you to make use of MATLAB's powerful graphical and data analysis capabilites.

Contents

 

Using XLSWRITE

The XLSWRITE function allows you to write data to a Excel spreadsheet. The best place to start is looking at the doc (type "doc xlswrite" at the command line). We are going to look at using the:

xlswrite('filename', M, sheet, 'range')

syntax, where:

  • filename: is the name of the Excel spreadsheet to which the data must be written.
  • M: is the data written to the spreadsheet and can be a numeric matrix, string or a cell array.
  • sheet: refers to the sheet inside excel to which the data goes.
  • range: refers to the spreadsheet area in which the data is put.

 

Here's an example:

% Create some arbitrary data, 5 columns of data each representing a
% separate variable.

M = rand(20,15);

% Create some column headers for the data
ColHeaders = {'Col 1','Col 2','Col 3','Col 4','Col 5'};

% Now write the data into the first sheet of a new spreadsheet
xlswrite('test_spreadseet', M, 1, 'A2:E21');

% Add the column headers
xlswrite('test_spreadseet', ColHeaders, 1, 'A1:E1');

The resulting spreadsheet is shown below:

Using XLSREAD

The XLSREAD function allows you to read data from a Excel spreadsheet. The syntax in this example is:

[num, txt] = xlsread('filename', sheet, 'range')

Where:

  • num: contains the numeric data in the specified range.
  • txt: contains the text data in the specified range.
  • filename: is the name of the Excel spreadsheet from which the data must be read.
  • sheet: refers to the sheet inside excel from which the data is read.
  • range: refers to the spreadsheet area which is extracted.

 

% Read the data from the previously created spreadsheet
[num,txt] = xlsread('test_spreadseet', 1, 'A1:E21');

% View the data
imagesc(num);
set(gca,'xtick',1:size(num,2),'xticklabel',txt)

Reading Excel Data Interactively

It is also possible to read Excel data into MATLAB interactively. The following command:

% Read the data from the previously created spreadsheet
[num,txt] = xlsread('test_spreadseet.xls', -1);

opens the spreadsheet of choice and allows you to select the range of data that will be read into MATLAB.

Once the data of interest has been selected you can click "OK" on the dialogue box to import that data into the MATLAB workspace.

Reading Excel Data Using the Import Wizard

Another interactive way of importing data into MATLAB is by using the Import Wizard. Double clicking on an Excel spreadsheet from MATLAB's Current Directory browser activates this GUI:

The numeric data can either be read in as a matrix or the Import Wizard can import the data as separate variables specified by the spreadsheet column headings.

Reading and Writing Excel Data From the Array Editor

The MATLAB Array Editor also provides Excel Copy and Paste compatibility. You can open the Array Editor by double clicking on a variable in the MATLAB Workspace Browser. Data can be copied from the Array Editor to a spreadsheet or from a spreadsheet to the Array Editor, maintaining the format.