Main Content

Read Cell Arrays of .NET 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. For more examples, see Convert Returned .NET Data to MATLAB Cell Arrays.

Create a file in Excel that contains this 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, and select Format Cells. On the Number tab, verify that the value for Category is Date.

Save the file as weight.xls in a local folder, for example, H:\Documents\MATLAB folder. Then close the file.

In MATLAB, read the data from the spreadsheet.

NET.addAssembly("microsoft.office.interop.excel");
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.

cellfun(@disp,data(:,1))
Date       
   31-Oct-1996 00:00:00

   29-Nov-1996 00:00:00

   30-Dec-1996 00:00:00

   31-Jan-1997 00:00:00

When you are finished with the example, quit the Excel program.

Close(book)
Quit(app)

See Also

Topics