Data getting lost or not recognized with datenum and datetime
3 views (last 30 days)
Show older comments
Carlos Castillo
on 9 Jun 2016
Commented: Stephen23
on 10 Jun 2016
Hello,
I need to plot data from a .csv file with respect to time, which has been saved in the format 'MM/dd/yyyy hh:mm:ss.ss', here's a screenshot of the array and another of the concatenated dates and times


I first tried to convert them to serial date numbers and it returned an error saying that it doesn't recognize or can't parse it with the actual format it has, it only worked when I changed the input format to 'MM/dd/yyyy hh:mm:ss'(no decimals for the seconds). Now, this conversion apparently rounds the value because I got several rows with the same number and a lot of data was lost for the plot. I was reading the documentation and I see that for example 6pm would be the day plus 0.75, so if it doesn't really account for minutes or seconds that explains why I get the same value in a lot of consecutive rows.
Then I tried to convertthem to datetime values with
time_obj = datetime(datetimes, 'InputFormat', 'MM/dd/yyyy hh:mm:ss.ss');
Note that in this case datetime does accept the actual input format. But now a lot of data was corrupted and lost, here is a screenshot of the array that was created.

You can see there that the values jump back and forth when I don't get a Not-a-Time result. Why does this happen? Does anyone know how to fix this for this specific date and time format in order to plot it wihout losing data?
Thanks in advance.
0 Comments
Accepted Answer
Walter Roberson
on 9 Jun 2016
It is difficult to tell for sure from the image, but it appears to me that in your file in the case where the fraction of a second is a single digit, that you have a space before the digit, as if the fraction of a second had been output by using the format '%2d' instead of '%02d' . If I am correct, then those entries with the space cannot be parsed automatically and you will either need to adjust the input file or you will need to write code to parse and convert the input.
2 Comments
Stephen23
on 10 Jun 2016
Did you read the documentation? According to the datetime properties, the correct format for fractions of seconds is capital S. You are using lowercase s, which MATLAB correctly interprets as meaning the seconds. Try using ... hh:mm:ss.SS.
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!