Import Dates and Times from Text Files

Import formatted dates and times (such as '01/01/01' or '12:30:45') from column oriented tabular data in three ways.

  • Import Tool — Interactively select and import dates and times.

  • readtable function — Automatically detect variables with dates and times and import them into a table.

  • Import Options — Use readtable with detectImportOptions function for more control over importing date and time variables. For example, you can specify properties such as FillValue and DatetimeFormat.

This example shows you how to import dates and times from text files using each of these methods.

Import Tool

Open the file outages.csv using the Import Tool. Specify the formats of dates and times using the drop-down menu for each column. You can select from a predefined date format, or enter a custom format. To import the OutageTime column, specify the custom format yyyy-MM-dd HH:mm. Then, click the Import Selection button to import the data into the workspace.

readtable Function

Use the readtable function and display 10 rows of the OutageTime variable. readtable automatically detects the date time variables and formats.

filename = 'outages.csv';
T = readtable(filename);
T.OutageTime(1:10)
ans = 10x1 datetime
   2002-02-01 12:18
   2003-01-23 00:49
   2003-02-07 21:15
   2004-04-06 05:44
   2002-03-16 06:18
   2003-06-18 02:49
   2004-06-20 14:39
   2002-06-06 19:28
   2003-07-16 16:23
   2004-09-27 11:09

Import Options

Use an import options object for more control over importing date and time variables. For example, change the date-time display format or specify a fill value for missing dates.

Create an import options object for the outages.csv file and display the variable import options for the variable RestorationTime. The detectImportOptions function automatically detects the data types of the variables.

opts = detectImportOptions(filename);
getvaropts(opts,'RestorationTime')
ans = 
  DatetimeVariableImportOptions with properties:

   Variable Properties:
              Name: 'RestorationTime'
              Type: 'datetime'
         FillValue: NaT
    TreatAsMissing: {}
         QuoteRule: 'remove'
          Prefixes: {}
          Suffixes: {}
    EmptyFieldRule: 'missing'

   Datetime Options:
    DatetimeFormat: 'default'
    DatetimeLocale: 'en_US'
       InputFormat: ''
          TimeZone: ''

Import the data and display the first 10 rows of the variable RestorationTime. The second row contains a NaT, indicating a missing date and time value.

T = readtable(filename,opts);
T.RestorationTime(1:10)   
ans = 10x1 datetime
   2002-02-07 16:50
   NaT             
   2003-02-17 08:14
   2004-04-06 06:10
   2002-03-18 23:23
   2003-06-18 10:54
   2004-06-20 19:16
   2002-06-07 00:51
   2003-07-17 01:12
   2004-09-27 16:37

To use a different date-time display format, update the DatetimeFormat property, and then replace missing values with the current date and time by using the FillValue property. Display the updated variable options.

opts = setvaropts(opts,'RestorationTime', ...
                       'DatetimeFormat','MMMM d, yyyy HH:mm:ss Z',...
                       'FillValue','now');
getvaropts(opts,'RestorationTime')
ans = 
  DatetimeVariableImportOptions with properties:

   Variable Properties:
              Name: 'RestorationTime'
              Type: 'datetime'
         FillValue: February 29, 2020 00:15:22 *
    TreatAsMissing: {}
         QuoteRule: 'remove'
          Prefixes: {}
          Suffixes: {}
    EmptyFieldRule: 'missing'

   Datetime Options:
    DatetimeFormat: 'MMMM d, yyyy HH:mm:ss Z'
    DatetimeLocale: 'en_US'
       InputFormat: ''
          TimeZone: ''

Read the data with the updated import options and display the first 10 rows of the variable.

T = readtable(filename,opts);
T.RestorationTime(1:10)   
ans = 10x1 datetime
   2002-02-07 16:50
   2020-02-29 00:15
   2003-02-17 08:14
   2004-04-06 06:10
   2002-03-18 23:23
   2003-06-18 10:54
   2004-06-20 19:16
   2002-06-07 00:51
   2003-07-17 01:12
   2004-09-27 16:37

For more information on the datetime variable options, see the setvaropts reference page.

See Also

| | | | | | |

Related Topics