readcell

Read cell array from file

Syntax

C = readcell(filename)
C = readcell(filename,opts)
C = readcell(___,Name,Value)

Description

example

C = readcell(filename) creates a cell array by reading column-oriented data from a file.

readcell determines the file format from the file extension:

  • .txt, .dat, or .csv for delimited text files

  • .xls, .xlsb, .xlsm, .xlsx, .xltm, .xltx, or .ods for spreadsheet files

example

C = readcell(filename,opts) additionally uses the import options opts.

example

C = readcell(___,Name,Value) creates cell array from a file with additional options specified by one or more name-value pair arguments. Use any of the input arguments from the previous syntaxes before specifying the name-value pairs.

To set specific import options for your data, you can either use the opts object or you can specify name-value pairs. When you specify name-value pairs in addition to opts, then readcell supports only these name-value pairs:

  • Text Files — DateLocale, Encoding

  • Spreadsheet Files — Sheet, UseExcel

Examples

collapse all

Display the contents of basic_cell.txt, and then import the mixed data into a cell array.

type basic_cell.txt
1,2,3
hello,world,NaN
10-Oct-2018 10:27:56,1,
C = readcell('basic_cell.txt')
C = 3x3 cell array
    {[                   1]}    {[    2]}    {[        3]}
    {'hello'               }    {'world'}    {[      NaN]}
    {[10-Oct-2018 10:27:56]}    {[    1]}    {1x1 missing}

Import mixed tabular data from basic_cell.xls into a cell array.

C = readcell('basic_cell.xls')
C = 3x3 cell array
    {[                   1]}    {[    2]}    {[        3]}
    {'hello'               }    {'world'}    {1x1 missing}
    {[10-Oct-2018 10:27:56]}    {[    1]}    {1x1 missing}

Preview the data from a spreadsheet file and import the mixed data into a cell array from a specified sheet and range.

The spreadsheet file airlinesmall_subset.xlsx contains data in multiple worksheets for years between 1996 and 2008. Each worksheet has data for a given year. Preview the data from file airlinesmall_subset.xlsx. The preview function shows data from the from the first worksheet by default.

opts = detectImportOptions('airlinesmall_subset.xlsx');
preview('airlinesmall_subset.xlsx',opts)
ans=8×29 table
    Year    Month    DayofMonth    DayOfWeek    DepTime    CRSDepTime    ArrTime    CRSArrTime    UniqueCarrier    FlightNum    TailNum     ActualElapsedTime    CRSElapsedTime    AirTime    ArrDelay    DepDelay    Origin    Dest     Distance    TaxiIn    TaxiOut    Cancelled    CancellationCode    Diverted    CarrierDelay    WeatherDelay    SDelay    SecurityDelay    LateAircraftDelay
    ____    _____    __________    _________    _______    __________    _______    __________    _____________    _________    ________    _________________    ______________    _______    ________    ________    ______    _____    ________    ______    _______    _________    ________________    ________    ____________    ____________    ______    _____________    _________________

    1996      1          18            4         2117         2120        2305         2259           'HP'            415       'N637AW'           108                 99            85           6          -3       'COS'     'PHX'      551         5         18           0               ''              0             ''              ''           ''           ''                 ''        
    1996      1          12            5         1252         1245        1511         1500           'HP'            610       'N905AW'            79                 75            58          11           7       'LAX'     'PHX'      370         3         18           0               ''              0             ''              ''           ''           ''                 ''        
    1996      1          16            2         1441         1445        1708         1721           'HP'            211       'N165AW'            87                 96            74         -13          -4       'RNO'     'PHX'      601         4          9           0               ''              0             ''              ''           ''           ''                 ''        
    1996      1           1            1         2258         2300        2336         2335           'HP'           1245       'N183AW'            38                 35            20           1          -2       'TUS'     'PHX'      110         6         12           0               ''              0             ''              ''           ''           ''                 ''        
    1996      1           4            4         1814         1814        1901         1910           'US'            683       'N963VJ'            47                 56            34          -9           0       'DTW'     'PIT'      201         6          7           0               ''              0             ''              ''           ''           ''                 ''        
    1996      1          31            3         1822         1820        1934         1925           'US'            757       'N912VJ'            72                 65            52           9           2       'PHL'     'PIT'      267         6         14           0               ''              0             ''              ''           ''           ''                 ''        
    1996      1          18            4          729          730         841          843           'US'           1564       'N941VJ'            72                 73            58          -2          -1       'DCA'     'PVD'      357         3         11           0               ''              0             ''              ''           ''           ''                 ''        
    1996      1          26            5         1704         1705        1829         1839           'NW'           1538       'N960N'             85                 94            69         -10          -1       'DTW'     'RIC'      456         3         13           0               ''              0             ''              ''           ''           ''                 ''        

Import ten rows of data for variables 7, 8, and 9 from the worksheet named '2007'. The Excel range 'G2:I11' represents columns 7 through 9, and ten rows starting at row 2 after the variable names.

M = readcell('airlinesmall_subset.xlsx','Sheet','2007','Range','G2:I11')
M = 10x3 cell array
    {[ 935]}    {[ 935]}    {'WN'}
    {[1041]}    {[1040]}    {'WN'}
    {[1430]}    {[1500]}    {'WN'}
    {[ 940]}    {[ 950]}    {'WN'}
    {[1515]}    {[1515]}    {'WN'}
    {[2042]}    {[2035]}    {'WN'}
    {[2116]}    {[2130]}    {'WN'}
    {[1604]}    {[1605]}    {'WN'}
    {[1258]}    {[1230]}    {'WN'}
    {[1134]}    {[1145]}    {'WN'}

Input Arguments

collapse all

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

Depending on the location of your file, filename can take on one of these forms.

Location

Form

Current folder or folder on the MATLAB® path

Specify the name of the file in filename.

Example: 'myFile.txt'

File in a folder

If the file is not in the current folder or in a folder on the MATLAB path, then specify the full or relative path name in filename.

Example: 'C:\myFolder\myFile.xlsx'

Example: 'dataDir\myFile.txt'

Remote Location

If the file is stored at a remote location, then filename must contain the full path of the file specified as an internationalized resource identifier (IRI) of the form:

scheme_name://path_to_file/my_file.ext

Based on your remote location, scheme_name can be one of the values in this table.

Remote Locationscheme_name
Amazon S3™s3
Windows Azure® Blob Storagewasb, wasbs
HDFS™hdfs

For more information, see Work with Remote Data.

Example: 's3://bucketname/path_to_file/my_file.csv'

  • If filename includes the file extension, then the importing function determines the file format from the extension. Otherwise, you must specify the 'FileType' name-value pair arguments to indicate the type of file.

  • On Windows® systems with Microsoft® Excel® software, the importing function reads any Excel spreadsheet file format recognized by your version of Excel.

  • If your system does not have Excel for Windows or if you are using MATLAB Online™, the importing function operates with the UseExcel property set to false, and reads only .xls, .xlsx, .xlsm, .xltx, and .xltm files.

  • For delimited text files, the importing function converts empty fields in the file to either NaN (for a numeric variable) or an empty character vector (for a text variable). All lines in the text file must have the same number of delimiters. The importing function ignores insignificant white space in the file.

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 the properties of each object, see the appropriate object page.

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

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'NumHeaderLines',5 indicates that the first five lines that precede the tabular data are header lines.

Text and Spreadsheet Files

collapse all

Type of file, specified as the comma-separated pair consisting of 'FileType' and 'text' or 'spreadsheet'.

Specify the 'FileType' name-value pair argument when the filename does not include the file extension or if the extension is other than one of the following:

  • .txt, .dat, or .csv for delimited text files

  • .xls, .xlsb, .xlsm, .xlsx, .xltm, .xltx, or .ods for spreadsheet files

Example: 'FileType','text'

Data Types: char | string

Number of header lines in the file, specified as the comma-separated pair consisting of 'NumHeaderLines' and a positive integer. If unspecified, the importing function automatically detects the number of header lines in the file.

Example: 'NumHeaderLines',7

Data Types: single | double

Expected number of variables, specified as the comma-separated pair consisting of 'ExpectedNumVariables' and a positive integer. If unspecified, the importing function automatically detects the number of variables.

Data Types: single | double

Portion of the data to read from text or spreadsheet files, specified as the comma separated pair consisting of 'Range' and a character vector, string scalar, or numeric vector in one of these forms.

Ways to specify RangeDescription

Starting Cell

'Cell' or [row col]

Specify the starting cell for the data as a character vector or string scalar or a two element numeric vector.

  • Character vector or string scalar containing a column letter and row number using Excel A1 notation. For example, A5 is the identifier for the cell at the intersection of column A and row 5.

  • Two element numeric vector of the form [row col] indicating the starting row and column.

Using the starting cell, the importing function automatically detects the extent of the data by beginning the import at the start cell and ending at the last empty row or footer range.

Example: 'A5' or [5 1]

Rectangular Range

'Corner1:Corner2' or [r1 c1 r2 c2]

Specify the exact range to read using the rectangular range in one of these forms.

  • 'Corner1:Corner2' — Specify the range using Corner1 and Corner2 which are the two opposing corners that define the region to read in Excel A1 notation. For example, 'C2:N15'.

  • [r1 c1 r2 c2] — Specify the range using a four element numeric vector containing start-row, start-column, end-row, and end-column. For example, [2 3 15 13].

The importing function only reads the data contained in the specified range. Any empty fields within the specified range are imported as missing cells.

Row Range or Column Range

'Row1:Row2' or 'Column1:Column2'

Specify the range by identifying the beginning and ending rows using Excel row numbers.

Using the specified row range, the importing function automatically detects the column extent by reading from the first nonempty column to the end of the data, and creates one variable per column.

Example: '5:500'

Alternatively, specify the range by identifying the beginning and ending columns using Excel column letters or numbers.

Using the specified column range, the import function automatically detects the row extent by reading from the first nonempty row to the end of the data or the footer range.

The number of columns in the specified range must match the number specified in the ExpectedNumVariables property.

Example: 'A:K'

Starting Row Number

n

Specify the first row containing the data using the positive scalar row index.

Using the specified row index, the importing function automatically detects the extent of the data by reading from the specified first row to the end of the data or the footer range.

Example:5

Excel’s Named Range

'NamedRange'

In Excel, you can create names to identify ranges in the spreadsheet. For instance, you can select a rectangular portion of the spreadsheet and call it 'myTable'. If such named ranges exist in a spreadsheet, then the importing function can read that range using its name.

Example: 'Range','myTable'

Unspecified or Empty

''

If unspecified, the importing function automatically detects the used range.

Example: 'Range',''

Note: Used Range refers to the rectangular portion of the spreadsheet that actually contains data. The importing function automatically detects the used range by trimming any leading and trailing rows and columns that do not contain data. Text that is only white space is considered data and is captured within the used range.

Data Types: char | string | double

Type for imported text data, specified as the comma-separated pair consisting of 'TextType' and either 'char' or 'string'.

  • 'char' — Import text data into MATLAB as character vectors.

  • 'string' — Import text data into MATLAB as string arrays.

Example: 'TextType','char'

Type for imported date and time data, specified as the comma-separated pair consisting of 'DatetimeType' and one of these values: 'datetime', 'text', or 'exceldatenum'. The value 'exceldatenum' is applicable only for spreadsheet files, and is not valid for text files.

ValueType for Imported Date and Time Data
'datetime'

MATLAB datetime data type

For more information, see datetime.

'text'

If 'DatetimeType' is specified as 'text', then the type for imported date and time data depends on the value specified in the 'TextType' parameter:

  • If 'TextType' is set to 'char', then the importing function returns dates as a cell array of character vectors.

  • If 'TextType' is set to 'string', then the importing function returns dates as an array of strings.

'exceldatenum'

Excel serial date numbers

A serial date number is a single number equal to the number of days from a given reference date. Excel serial date numbers use a different reference date than MATLAB serial date numbers. For more information on Excel dates, see https://support.microsoft.com/en-us/kb/214330.

Data Types: char | string

Text Files Only

collapse all

Field delimiter characters, specified as a character vector, string scalar, cell array of character vectors, or string array.

Example: 'Delimiter','|'

Example: 'Delimiter',{';','*'}

Data Types: char | string | cell

Characters to treat as white space, specified as a character vector or string scalar containing one or more characters.

Example: 'Whitespace',' _'

Example: 'Whitespace','?!.,'

End-of-line characters, specified as a character vector, string scalar, cell array of character vectors, or string array.

Example: 'LineEnding','\n'

Example: 'LineEnding','\r\n'

Example: 'LineEnding',{'\b',':'}

Data Types: char | string | cell

Style of comments, specified as a character vector, string scalar, cell array of character vectors, or string array.

For example, to ignore the text following a percent sign on the same line, specify CommentStyle as '%'.

Example: 'CommentStyle',{'/*'}

Data Types: char | string | cell

Character encoding scheme associated with the file, specified as the comma-separated pair consisting of 'Encoding' and 'system' or a standard character encoding scheme name, such as one of the values in this table.

'Big5'

'ISO-8859-1'

'windows-847'

'Big5-HKSCS'

'ISO-8859-2'

'windows-949'

'CP949'

'ISO-8859-3'

'windows-1250'

'EUC-KR'

'ISO-8859-4'

'windows-1251'

'EUC-JP'

'ISO-8859-5'

'windows-1252'

'EUC-TW'

'ISO-8859-6'

'windows-1253'

'GB18030'

'ISO-8859-7'

'windows-1254'

'GB2312'

'ISO-8859-8'

'windows-1255'

'GBK'

'ISO-8859-9'

'windows-1256'

'IBM866'

'ISO-8859-11'

'windows-1257'

'KOI8-R'

'ISO-8859-13'

'windows-1258'

'KOI8-U'

'ISO-8859-15'

'US-ASCII'

 

'Macintosh'

'UTF-8'

 

'Shift_JIS'

 

Example: 'Encoding','system' uses the system default encoding.

Data Types: char | string

Output data type of duration data from text files, specified as the comma-separated pair consisting of 'DurationType' and either 'duration' or 'text'.

ValueType for Imported Duration Data
'duration'

MATLAB duration data type

For more information, see duration.

'text'

If 'DurationType' is specified as 'text', then the type for imported duration data depends on the value specified in the 'TextType' parameter:

  • If 'TextType' is set to 'char', then the importing function returns duration data as a cell array of character vectors.

  • If 'TextType' is set to 'string', then the importing function returns duration data as an array of strings.

Data Types: char | string

Locale for reading dates, specified as the comma-separated pair consisting of 'DateLocale' and a character vector or a string scalar of the form xx_YY, where:

  • YY is an uppercase ISO 3166-1 alpha-2 code indicating a country.

  • xx is a lowercase ISO 639-1 two-letter code indicating a language.

For a list of common values for the locale, see the Locale name-value pair argument for the datetime function.

When using the %D format specifier to read text as datetime values, use DateLocale to specify the locale in which the importing function should interpret month and day-of-week names and abbreviations.

If you specify the DateLocale argument in addition to opts the import options, then the importing function uses the specified value for the DateLocale argument, overriding the locale defined in the import options.

Example: 'DateLocale','ja_JP'

Procedure to handle consecutive delimiters, specified as one of the values in this table.

Consecutive Delimiters RuleBehavior
'split'Split the consecutive delimiters into multiple fields.
'join'Join the delimiters into one delimiter.
'error'Return an error and abort the import operation.

Data Types: char | string

Procedure to manage leading delimiters, specified as one of the values in this table.

Leading Delimiters RuleBehavior
'keep'Keep the delimiter.
'ignore'Ignore the delimiter.
'error'Return an error and abort the import operation.

Spreadsheet Files Only

collapse all

Sheet to read from, specified as an empty character array, a character vector or string scalar containing the sheet name, or a positive scalar integer denoting the sheet index. Based on the value specified for the Sheet property, the import function behaves as described in the table.

SpecificationBehavior
'' (default)Import data from the first sheet.
NameImport data from the matching sheet name, regardless of order of sheets in the spreadsheet file.
IntegerImport data from sheet in the position denoted by the integer, regardless of the sheet names in the spreadsheet file.

Data Types: char | string | single | double

Flag to start an instance of Microsoft Excel for Windows when reading spreadsheet data, specified as the comma-separated pair consisting of 'UseExcel' and either true, or false.

When reading from spreadsheet files on Windows platforms, if you do not want to start an instance of Microsoft Excel, then set the 'UseExcel' parameter to false.

You can set the 'UseExcel' parameter to one of these values:

  • true — The importing function starts an instance of Microsoft Excel when reading the file. This setting is the default for Windows systems with Excel installed.

  • false — The importing function does not start an instance of Microsoft Excel when reading the file. On Windows without Excel, Mac, and Linux®, the default value is false. When operating in this mode, the importing function functionality differs in the support of file formats and interactive features, such as formulas and macros.

UseExcel

true

false

Supported file formats

.xls, .xlsx, .xlsm, .xltx, .xltm, .xlsb, .ods

.xls, .xlsx, .xlsm, .xltx, .xltm

Support for interactive features, such as formulas and macros

Yes

No

Introduced in R2019a