MATLAB: How import data and change data and time format

dateformat stringtime

formatspec='%f%f%f%f%f%f%{MM-dd-yyyy HH:mm:ss}D%{MM-dd-yyyy HH:mm:ss.SSSSSS}D'
formatspec=variable
%f- format for MATLAB, floating decimal, not sure why so many
{}-desired format for code
D%- no idea
D- no idea
If someone could please help me to understand what this string means, I could appreciate it. I received help on this, but cannot remember why we developed the string in this way.

Best Answer

%{....}D is a date specification. %{MM-dd-yyyy HH:mm:ss}D is equivalent to asking to
datetime(TheInputString, 'Format', 'MM-dd-yyyy HH:mm:ss')
which is numeric month, numeric day of month, four digit year, 24-hour type hour, two digit minute, two digits seconds.
and %{MM-dd-yyyy HH:mm:ss.SSSSSS}D is equivalent to asking to
datetime(TheInputString, 'Format', 'MM-dd-yyyy HH:mm:ss.SSSSSS')
which is numeric month, numeric day of month, four digit year, 24-hour type hour, two digit minute, two digits seconds, 6 digit fractions of a second.
Note: if you are using textscan then the %{}D formats given that way can have difficulty if the Whitespace parameter includes space: MATLAB would generally see the space as indicating the end of the field before trying to parse it as a date. readtable() is better at handling that.