MATLAB: How to convert a string to a date

datedatetimeformattime

Currently I have the string:
2017-03-18 03:26:42.000000
I want to convert this to a date, and I use the following function:
datetime(my_string,'InputFormat','yyyy-mm-dd HH:MM:SS')
However, when I do this I get the following error:
Unable to parse '2017-03-18 03:26:42.000000' as a date/time using the format 'yyyy-mm-dd HH:MM:SS'.
I have also tried
datetime(my_string,'InputFormat','yyyy-mm-dd HH:MM:SSSSSSSSS')
and
datetime(my_string,'InputFormat','yyyy-mm-dd HH:MM:SS.SSSSSSS')
But none of these seem to work. How would I go about doing this?

Best Answer

Carefully read the documentation of the Format property. The month identifier is uppercase M and the minute identifier is lowercase m. The seconds field is lowercase s with the fractional part as uppercase S. So:
datetime(my_string,'InputFormat','yyyy-MM-dd HH:mm:ss.S')
if you want to display the fractional seconds by default:
datetime(my_string,'InputFormat','yyyy-MM-dd HH:mm:ss.S', 'Format', 'yyyy-MM-dd HH:mm:ss.SSS')