Import a Worksheet or Range

Read Column-Oriented Data into Table

This example shows how to import mixed numeric and text data from a spreadsheet into a table, using the readtable function. Tables are suitable for column-oriented or tabular data. You can store variable names or row names along with the data in a single container.

This example uses a sample spreadsheet file, climate.xlsx, that contains the following numeric and text data in a worksheet called Temperatures.

Time		Temp		Visibility
12		98		clear
13		99		clear
14		97		partly cloudy

Create the sample file for reading.

d = {'Time','Temp','Visibility';
     12 98 'clear';
     13 99 'clear';
     14 97 'partly cloudy'};

xlswrite('climate.xlsx',d,'Temperatures');

xlswrite warns that it has added a worksheet.

Call readtable to read all the data in the worksheet called Temperatures. Specify the worksheet name using the Sheet name-value pair argument. If your data is on the first worksheet in the file, you do not need to specify Sheet.

T = readtable('climate.xlsx','Sheet','Temperatures')
T = 

    Time    Temp      Visibility   
    ____    ____    _______________

    12      98      'clear'        
    13      99      'clear'        
    14      97      'partly cloudy' 

readtable returns a 3-by-3 table. By default, readtable reads the first row of the worksheet as variable names for the table.

Read only the first two columns of data by specifying a range, 'A1:B4'.

cols = readtable('climate.xlsx','Sheet','Temperatures','Range','A1:B4')
cols = 

    Time    Temp
    ____    ____

    12      98  
    13      99  
    14      97  

readtable returns a 3-by-2 table.

Read Numeric and Text Data into Arrays

This example shows how to import mixed numeric and text data into separate arrays in MATLAB®, using the xlsread function.

This example uses a sample spreadsheet file, climate.xlsx, that contains the following data in a worksheet called Temperatures.

   Time    Temp
      12     98
      13     99
      14     97

Create the sample file for reading.

d = {'Time', 'Temp';
     12 98;
     13 99;
     14 97}

xlswrite('climate2.xlsx',d,'Temperatures');

xlswrite warns that it has added a worksheet.

Import only the numeric data into a matrix, using xlsread with a single output argument. xlsread ignores any leading row or column of text in the numeric result.

num = xlsread('climate2.xlsx','Temperatures')
num =
    12    98
    13    99
    14    97

xlsread returns the numeric array, num.

Alternatively, import both numeric data and text data, by specifying two output arguments in the call to xlsread.

[num,headertext] = xlsread('climate2.xlsx','Temperatures')
num =
    12    98
    13    99
    14    97

headertext = 
    'Time'    'Temp'

xlsread returns the numeric data in the array, num, and the text data in the cell array, headertext.

Read only the first row of data by specifying a range, 'A2:B2'.

row1 = xlsread('climate2.xlsx','Temperatures','A2:B2')
row1 =
    12    98

Get Information about a Spreadsheet

To determine whether a file contains a readable Excel® spreadsheet, use the xlsfinfo function. For readable files, xlsfinfo returns a nonempty string, such as 'Microsoft Excel Spreadsheet'. Otherwise, it returns an empty string ('').

You also can use xlsfinfo to identify the names of the worksheets in the file, and to obtain the file format reported by Excel. For example, retrieve information about the spreadsheet file, climate2.xlsx, created in the previous example:

[type, sheets] = xlsfinfo('climate2.xlsx')

type =
Microsoft Excel Spreadsheet
sheets = 
    'Sheet1'    'Sheet2'    'Sheet3'    'Temperatures'

See Also

| |

More About

Was this topic helpful?