Main Content

Import Spreadsheets

Spreadsheets often contain a mix of numeric and text data as well as variable and row names, which is best represented in MATLAB® as a table. You can import data into a table using the Import Tool or the readtable function.

Import Spreadsheet Data Using the Import Tool

The Import Tool allows you to import into a table or other data type. For example, read data from the sample spreadsheet file patients.xls as a table in MATLAB. Open the file using the Import Tool and select options such as the range of data and the output type. Then, click the Import Selection button to import the data into the MATLAB workspace.

Import Spreadsheet Data Using readtable

Alternatively, you can read spreadsheet data into a table using the readtable function with the file name, for example:

T = readtable('patients.xls');

You can also select the range of data to import by specifying the range parameter. For example, read the first five rows and columns of the spreadsheet. Specify the range in Excel notation as 'A1:E5'.

T = readtable('patients.xls','Range','A1:E5')
T =

  4×5 table

      LastName        Gender      Age              Location               Height
    ____________    __________    ___    _____________________________    ______

    {'Smith'   }    {'Male'  }    38     {'County General Hospital'  }      71  
    {'Johnson' }    {'Male'  }    43     {'VA Hospital'              }      69  
    {'Williams'}    {'Female'}    38     {'St. Mary's Medical Center'}      64  
    {'Jones'   }    {'Female'}    40     {'VA Hospital'              }      67  

Import Spreadsheet Data as Other Data Types

In addition to tables, you can import your spreadsheet data into the MATLAB workspace as a timetable, a numeric matrix, a cell array, or separate column vectors. Based on the data type you need, use one of these functions.

Data Type of Output Function
Timetablereadtimetable
Numeric Matrixreadmatrix
Cell Arrayreadcell
Separate Column Vectorsreadvars

See Also

|

Related Topics