I'm noticing a some strange things with a MATLAB function I am using. It seems to be missing the hour, minute, and seconds in the calculation.
posixtime(t) takes a datetime value t and should output the "number of seconds elapsed since 00:00:00 1-Jan-1970 UTC
I am reading in the correct datetime values using thingSpeakRead. If I print the string in the console for the first value, I get exactly:
Sounds good. Okay, now I try to apply the MATLAB function posixtime(t) to get the number of seconds for the value above, and it outputs:
1.4422e+09 (1442200000 seconds)
Which is according to human readable format: GMT: Mon, 14 Sep 2015 03:06:40 GMT
The date seems to be fairly accurate (probably off by a timezone), but the hour/minutes/seconds don't make sense. I am thinking that the MATLAB function has thrown away the HH:MM:SS and not performing the calculation.
I don't think this is me...I also tried a different function for calculating time and got some issues. I used datenum(t) and it also forgets to add hours, minutes, seconds. Further out, I used datenum(t, formatIn) to try to force the timestamp format and the console gave me this error:
Error using datetime/datenum
Too many input arguments.
Can someone look into this?
We'll look into this, and try to understand what's going on.
That said, in general, I'd strongly recommend working with the (reasonably new) datetime object, which understands timezones and has a wide array of functions for manipulating dates and times. This is what ThingSpeakRead retuns. Many of the plot functions natively understand it as well, and "do the right thing" when you pass them in.
Going a little bit off-topic here for my specific project, but how would you recommend me using the MATLAB trapz(x,y) function to look at the area under the curve of my data over time? Where X is my data and Y would be my datetime object that thingSpeakRead outputs. Obviously the trapz function doesn't take datetime objects. I tried that first thing which is why I started looking into changing my datetime value to epoch seconds. That is an easy number for MATLAB to calculate which is why I went that route.
trapz, integral, integral2, intelgra3 functions don't take datetime objects. Damn!
I tried this:
x = datetime('now')
y = posixtime(x)
z = datetime(y,'ConvertFrom','posixtime')
Which is what I would expect....
If you're trying to calculate elapsed seconds between two datetimes, just subtract them, which will give you a duration object, and you can convert that to seconds:
>> elapsedTimeInDay = datetime('now') - datetime('today')
Thank you for your help, Rob. I think I see what is going on here and my spirits have been lifted! I was using the console to double-check my work and did not realize that it wasn't outputting the entire double number in the console screen.
The console keeps outputting: 1.4422e+09
And I am expecting more decimal places: 1442245952
I never tried: z = datetime(y,'ConvertFrom','posixtime') to read back as a datetime object.
Not sure if you can increase the number of decimal places in the console screen. I would assume that somebody might run into a similar situation as I did and just assume. My mistake - sorry!
Glad you figured out the console display thing. MATLAB is generally optimized to print out matrixes, so it tends to choose terse display formats by default. As Adarsh says, you can set the display format.
On trapz, I broke my answer into a seperate topic on the forum so other people might see it:
Most Users Ever Online: 114
Currently Online: cstapels
Currently Browsing this Page:
Guest Posters: 1
Administrators: Hans, lee