Support Articles
Technical Computing
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.
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:
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:

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:
% 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)

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