2:34 pm

Silver

June 18, 2015

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!

3:47 pm

MathWorks

August 22, 2015

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!

3:53 pm

Silver

June 18, 2015

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!

3:58 pm

MathWorks

August 22, 2015

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!

4:19 pm

Silver

June 18, 2015

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!

4:47 pm

MathWorks

August 25, 2015

1:56 pm

MathWorks

August 22, 2015

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!

Most Users Ever Online: 166

Currently Online:

15 Guest(s)

Currently Browsing this Page:

2 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

khanhNguyen_CT, susyburr, quijano__, meosuckhoe, briskpanda, Dejw0089Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457