Main Content


Preview eight rows from file using import options



T = preview(filename,opts) returns a table containing the first eight rows of data in filename, using the import options opts.

  • The value of the SelectedVariableNames property of the import options object opts selects the variables that appear in the table T.

  • The preview function returns a subset of the data that the readtable function returns with the import options object opts.

  • Usually, the output of the preview function contains eight rows of data. However, in some instances the number of rows differs depending on property values defined in the import options object opts. For more details see, Tips.


collapse all

First, create an import options object. Next, use the import options to get the first eight rows of the table, and then generate a preview showing only the variables of interest.

Create an import options object for a file.

filename = 'outages.csv';
opts = detectImportOptions(filename);

Preview the table.

ans=8×6 table
      Region            OutageTime          Loss     Customers       RestorationTime             Cause      
    ___________    ____________________    ______    __________    ____________________    _________________

    'SouthWest'    01-Feb-2002 12:18:00    458.98    1.8202e+06    07-Feb-2002 16:50:00    'winter storm'   
    'SouthEast'    23-Jan-2003 00:49:00    530.14    2.1204e+05                     NaT    'winter storm'   
    'SouthEast'    07-Feb-2003 21:15:00     289.4    1.4294e+05    17-Feb-2003 08:14:00    'winter storm'   
    'West'         06-Apr-2004 05:44:00    434.81    3.4037e+05    06-Apr-2004 06:10:00    'equipment fault'
    'MidWest'      16-Mar-2002 06:18:00    186.44    2.1275e+05    18-Mar-2002 23:23:00    'severe storm'   
    'West'         18-Jun-2003 02:49:00         0             0    18-Jun-2003 10:54:00    'attack'         
    'West'         20-Jun-2004 14:39:00    231.29           NaN    20-Jun-2004 19:16:00    'equipment fault'
    'West'         06-Jun-2002 19:28:00    311.86           NaN    07-Jun-2002 00:51:00    'equipment fault'

Select which variables to include in the preview and read operations.

opts.SelectedVariableNames = {'Region','OutageTime'};

Preview the first eight rows and selected variables.

ans=8×2 table
      Region            OutageTime     
    ___________    ____________________

    'SouthWest'    01-Feb-2002 12:18:00
    'SouthEast'    23-Jan-2003 00:49:00
    'SouthEast'    07-Feb-2003 21:15:00
    'West'         06-Apr-2004 05:44:00
    'MidWest'      16-Mar-2002 06:18:00
    'West'         18-Jun-2003 02:49:00
    'West'         20-Jun-2004 14:39:00
    'West'         06-Jun-2002 19:28:00

Input Arguments

collapse all

Name of the file to read, specified as a character vector or a string scalar.

Example: 'myFile.xlsx'

Data Types: char | string

File import options, specified as an SpreadsheetImportOptions, DelimitedTextImportOptions or FixedWidthImportOptions object created by the detectImportOptions function. The opts object contains properties that control the data import process. For more information on properties for individual objects, see the appropriate object page.

Type of FilesOutput
Spreadsheet filesSpreadsheetImportOptions object
Text filesDelimitedTextImportOptions object
Fixed-width text filesFixedWidthImportOptions object


  • The preview function returns fewer than eight rows in these cases:

    • When the file contains fewer than eight rows of data.

    • When the DataLines or the DataRange properties of the opts object specify less than eight rows.

    • When a combination of the properties of the opts object result in less than eight rows of data. For example, preview returns less than eight rows when the value of the EmptyLineRule, ImportErrorRule or the MissingRule of the opts object causes the importing function to omit data.

  • When the DataLines or the DataRange property of the opts object specifies multiple row ranges and the first row range spans fewer than eight rows, then preview returns the rows from the first row range followed by rows from successive row ranges until it reaches a total of eight rows.

Version History

Introduced in R2018a