1:55 pm

MathWorks

August 22, 2015

fishmastaflex asked a question about using the trapz function in MATLAB to calculate the average temperature on a channel where the samples come in irregularly. There's no built in function to do this, but it's only a couple lines of code.

I have a similar challenge with one of my temperature sensors. It only sends data when there is a significant change:

>> data = thingSpeakRead(32188,'NumPoints',5,'OutputFormat','table')

data =

Timestamps TempF

____________________ ______

15-Sep-2015 13:28:33 68.584

15-Sep-2015 13:29:34 68.685

15-Sep-2015 13:30:34 68.861

15-Sep-2015 13:34:36 69.047

15-Sep-2015 13:36:36 69.188

To use trapz, you need to convert the timestamps into X coordinates. Do this by calculating the relative time for each timestamp in seconds:

X = seconds(data.Timestamps - data.Timestamps(1))

X =

0

61

121

363

483

Calculate the area under the curve (approximate integral) using trapz, then divide by the number of elapsed seconds to get the mean average:

>> avg = trapz(X,data.TempF) / X(end)

avg =

68.9317

More about trapz: http://www.mathworks.com/help/matlab/ref/trapz.html

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:30 pm

Silver

June 18, 2015

Thanks Rob. Glad to spark the conversation.

My method works pretty good too using the posix time function. It's only one line of code:

areaUnderCurve = trapz(posixtime(X),Y);

X is my matrix of timestamps

Y is my data

Watch out for NaN values though as well. Even if you have just one, the trapz function will spit out NaN. I used isnan to delete values in the matrix.

7:17 pm

MathWorks

August 22, 2015

I like it.

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:

14 Guest(s)

Currently Browsing this Page:

1 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