This example shows how to aggregate data to a lower time resolution in a ThingSpeak™ channel to remove irregularity. Irregularity in a data can be caused due to several factors such as event driven sensing, malfunctioning of sensors, or network latencies.
ThingSpeak channel 22641 contains tide
and weather data measured once a minute at Ockway Bay, Cape Cod. Field
2 of the channel contains air temperature data. Read the air temperature
data for the past 3 hours from channel 22641 using the
datetimeStop = dateshift(datetime('now'),'start','hour'); datetimeStart = dateshift(datetime('now'),'start','hour') - hours(3); data = thingSpeakRead(22641,'DateRange',[datetimeStart,datetimeStop],... 'Fields',2,'outputFormat','timetable');
Data is measured once every minute. However,
due to network latency associated with the measurement system, the
actual timestamps might be greater than or less than a minute apart.
Further, for the application of interest, the frequency of data measured
every minute is high. Data at an hourly time resolution is sufficient.
You can use the
retime function to aggregate the
data for each hour to a single value. You can use the maximum value
for each hour to aggregate the data.
dataHourly = retime(data,'hourly','max');
Send the aggregated data to a ThingSpeak channel
% Change the Channel ID and the '<write API key>' to send data to your channel thingSpeakWrite(<channelID>,chirpData,'WriteKey','<write API key>');