MATLAB: How to import a datetime vector of the form “yyyy-MM-dd HH:mm:ss.000”

csvdatetimeimport_toolMATLAB

I have a CSV file with a column that has the form "yyyy-MM-dd HH:mm:ss.000", excluding the quotation marks. How can I import this as a datetime array?
See the attached ExampleCSV for an example of this file.

Best Answer

You can use the Import Tool to import this data type.
1. On the Home tab of MATLAB, click on Import Data. Navigate to and select the file that you would like to import. This answer uses the provided ExampleCSV.csv file.
2. In the Import Tool window, find the column with this particular data. From the drop-down menu directly above Row 1 of the table, choose 'More Date Formats'. Then enter the custom format as "yyyy-MM-dd HH:mm:ss.000" without the quotation marks. Click the green check mark to import the table.
The datetime data can now be accessed by executing the code
>> t = ExampleCSV.MyTime(2:end);
Note that the 'dot notation' here is used to index into the 'table' data type.