Documentation Center

  • Trial Software
  • Product Updates

Import All Worksheets from a File

Import Numeric Data from All Worksheets

This example shows how to import worksheets in an Excel® file that contains only numeric data (no row or column headers, and no inner cells with text) into a structure array, using the importdata function.

Create a sample spreadsheet file for importing by writing an array of numeric data to the first and second worksheets in a file called numdata.xlsx.

xlswrite('numdata.xlsx',rand(5,5),1);
xlswrite('numdata.xlsx',rand(5,6),2);

Import the data from all worksheets in numdata.xlsx.

S = importdata('numdata.xlsx')
S = 

    Sheet1: [5x5 double]
    Sheet2: [5x6 double]

importdata returns a structure array, S, with one field for each worksheet with data.

Import Data and Headers from All Worksheets

This example shows how to read numeric data and text headers from all worksheets in an Excel file into a nested structure array, using the importadata function.

This example uses a sample spreadsheet file, testdata.xlsx, that contains the following data in the first worksheet, and similar data in the second worksheet.

   Time    Temp
      12     98
      13     99
      14     97

Write a sample file, testdata.xlsx, for reading. Write an array of sample data, d1, to the first worksheet in the file and a second array, d2, to the second worksheet.

d1 = {'Time','Temp';
     12 98;
     13 99;
     14 97};
 
d2 = {'Time','Temp';
     12 78;
     13 77;
     14 78};

xlswrite('testdata.xlsx',d1,1);
xlswrite('testdata.xlsx',d2,2);

Read the data from all worksheets in testdata.xlsx.

climate = importdata('testdata.xlsx')
climate = 

          data: [1x1 struct]
      textdata: [1x1 struct]
    colheaders: [1x1 struct]

importdata returns a nested structure array, climate, with three fields, data, textdata, and colheaders. Structures created from Excel files with row headers include the field rowheaders instead of colheaders.

View the contents of the structure array named data.

climate.data
ans = 

    Sheet1: [3x2 double]
    Sheet2: [3x2 double]

climate.data contains one field for each worksheet with numeric data.

View the data in the worksheet named Sheet1.

climate.data.Sheet1
ans =

    12    98
    13    99
    14    97

The field, Sheet1, contains the numeric data from the first worksheet in the file.

View the column headers in each sheet.

headers = climate.colheaders
headers = 

    Sheet1: {'Time'  'Temp'}
    Sheet2: {'Time'  'Temp'}

Both the worksheets named Sheet1 and Sheet2 have the column headers, Time and Temp.

See Also

More About

Was this topic helpful?