Data getting lost or not recognized with datenum and datetime

3 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2016-6-9
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 个评论
Carlos Castillo
Carlos Castillo 2016-6-10
That was the problem. Well, one of the problems. I eliminated all the empty spaces and still the conversion was wrong, they values kept jumping back and forth. When I looked closely I realized that MATLAB was converting not the integer parts and then rounding them; it was converting only the decimals and returning them as seconds. The integer part was lost. I solved this by eliminating the decimals and working only with integers—by hand in Excel.
Do you know what is happening here?
Thanks for your answer.
Stephen23
Stephen23 2016-6-10
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by