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

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





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

sp_Feed sp_TopicIcon
POSIX (Number of days since) not working
No permission to create posts
September 14, 2015
2:34 pm
Avatar
fishmastaflex

Silver
Members
Forum Posts: 11
Member Since:
June 18, 2015
sp_UserOfflineSmall Offline

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!

September 14, 2015
3:47 pm
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 46
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

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!

September 14, 2015
3:53 pm
Avatar
fishmastaflex

Silver
Members
Forum Posts: 11
Member Since:
June 18, 2015
sp_UserOfflineSmall Offline

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!

September 14, 2015
3:58 pm
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 46
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

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!

September 14, 2015
4:19 pm
Avatar
fishmastaflex

Silver
Members
Forum Posts: 11
Member Since:
June 18, 2015
sp_UserOfflineSmall Offline

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!

September 14, 2015
4:47 pm
Avatar
Adarsh_Murthy

MathWorks
Members
Forum Posts: 62
Member Since:
August 25, 2015
sp_UserOfflineSmall Offline

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

September 15, 2015
1:56 pm
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 46
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

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!

September 15, 2015
3:31 pm
Avatar
fishmastaflex

Silver
Members
Forum Posts: 11
Member Since:
June 18, 2015
sp_UserOfflineSmall Offline

Good call, cheers!

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
16 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

BarryMaync, ThomasRougs, DonaldGen, prince, JrGordon, Elamsweems

Administrators: Hans: 387, lee: 457