POSIX (Number of days since) not working | MATLAB | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
POSIX (Number of days since) not working
Avatar
fishmastaflex

Silver
Forum Posts: 11
sp_UserOfflineSmall Offline
1
September 14, 2015 - 2:34 pm
sp_Permalink sp_Print

Hey guys,

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
http://www.mathworks.com/help/matlab/ref/posixtime.html

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:
13-Sep-2015 18:20:58

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:
datenum(t,'dd-mmm-yyyy HH:MM:SS')
Error using datetime/datenum
Too many input arguments.

Can someone look into this?

Thanks!

Avatar
Rob Purser
Natick, MA

Forum Posts: 46
sp_UserOfflineSmall Offline
2
September 14, 2015 - 3:47 pm
sp_Permalink sp_Print

Hi,

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.

More on datetime: http://www.mathworks.com/help/matlab/ref/datetime.html and http://www.mathworks.com/help/matlab/date-and-time-operations-1.html

-Rob

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

Avatar
fishmastaflex

Silver
Forum Posts: 11
sp_UserOfflineSmall Offline
3
September 14, 2015 - 3:53 pm
sp_Permalink sp_Print

Thanks Rob,

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!

Avatar
Rob Purser
Natick, MA

Forum Posts: 46
sp_UserOfflineSmall Offline
4
September 14, 2015 - 3:58 pm
sp_Permalink sp_Print

Hi fishmastaflex,

I tried this:

x = datetime('now')
y = posixtime(x)
z = datetime(y,'ConvertFrom','posixtime')

And got:
x =
14-Sep-2015 15:52:32

y =
1.4422e+09

z =
14-Sep-2015 15:52:32

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')

elapsedTimeInDay =
15:57:09

>> seconds(elapsedTimeInDay)
ans =
5.7429e+04

>> hours(elapsedTimeInDay)
ans =
15.9526

>> days(elapsedTimeInDay)
ans =
0.6647

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

Avatar
fishmastaflex

Silver
Forum Posts: 11
sp_UserOfflineSmall Offline
5
September 14, 2015 - 4:19 pm
sp_Permalink sp_Print

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!

Avatar
Adarsh_Murthy

Forum Posts: 59
sp_UserOfflineSmall Offline
6
September 14, 2015 - 4:47 pm
sp_Permalink sp_Print

Hi fishmastaflex,

That is a good point. I use the "format long g" command to display the a number in a non-exponential format, i.e.,

dt = datetime('now')
format long g
posixtime(dt)

ans =

1442248971.20384

- Adarsh

Avatar
Rob Purser
Natick, MA

Forum Posts: 46
sp_UserOfflineSmall Offline
7
September 15, 2015 - 1:56 pm
sp_Permalink sp_Print

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:
http://community.thingspeak.com/forum/matlab/solution-calculating-the-average-of-channel-with-uneven-timestamps-using-trapz/

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

Avatar
fishmastaflex

Silver
Forum Posts: 11
sp_UserOfflineSmall Offline
8
September 15, 2015 - 3:31 pm
sp_Permalink sp_Print

Good call, cheers!

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: cstapels
17 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

Nsneilotof

angieus2

goldiepw11

BinanceoSt

gnarender123@gmail.com

edenderest

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee