Main Content

Read Cell Arrays of Excel Spreadsheet Data

This example for Microsoft® .NET Framework shows how to convert columns of Microsoft Excel® spreadsheet data to MATLAB® types. MATLAB reads a range of .NET values as a System.Object[,] type. Use the cell function to convert System.String values to MATLAB character arrays and System.DateTime values to datetime objects.

Create a file in Excel that contains the following data.

Date       Weight
10/31/96   174.8
11/29/96   179.3
12/30/96   190.4
01/31/97   185.7

Right-click the Date column, select Format Cells, and then the Number tab. Verify that the value for Category: is Date.

Name the file weight.xls in the H:\Documents\MATLAB folder. Close the file.

In MATLAB, read the data from the spreadsheet.

app = Microsoft.Office.Interop.Excel.ApplicationClass;
book =  app.Workbooks.Open('H:\Documents\MATLAB\weight.xls');
sheet = Microsoft.Office.Interop.Excel.Worksheet(book.Worksheets.Item(1)); 
range = sheet.UsedRange;
arr = range.Value;

Convert the data to MATLAB types.

data = cell(arr,'ConvertTypes',{'all'});

Display the dates.

   31-Oct-1996 00:00:00

   29-Nov-1996 00:00:00

   30-Dec-1996 00:00:00

   31-Jan-1997 00:00:00

Quit the Excel program.


Related Examples

More About