Hi,
I'm quite new to the field of manipulating my data and can't find any examples of my goal.
I have a channel with a field (fieldMotion) where I receive data from a motionsensor where I simply write ThingSpeak.setField(3, 1) for every motion detected. I would like to create a bar chart (in a Visualization) where I sum every 1's within an hour to show amount of motions per hour for a day. Also I would like to do the same per day for a week. Could anybody please help me with a working code I could use to make this work?

 채택된 답변

Christopher Stapels
Christopher Stapels 2020년 10월 13일

0 개 추천

Use ThingSpeakRead to get your data, i reccomend a timetable:
channelID=12397;
data = thingSpeakRead(channelID,'Fields',[1,4],'NumPoints',3,'OutputFormat','TimeTable');
% might have to add api key for private channels
Use retime to create the sum
sumData=retime(data,'hourly','sum'); % I think daily is also possible for your second request.
plot your bar chart
bar(sumData.Timestamps,sumData.TemperatureF);
You will probably have to change TemperatureF to the name of your field. Leave the semicolon off the first command to see the varaible names when you run it.
Then add it to the channel where you want to see it.

댓글 수: 4

Stigh Aarstein
Stigh Aarstein 2020년 10월 13일
편집: Stigh Aarstein 2020년 10월 13일
Hi Christopher,
This code works great to make the daily bar chart.
channelID=XXX;
readAPIKey = 'YYY';
fieldID = 3;
oneDay = [datetime('today') datetime('now')];
data = thingSpeakRead(channelID,'Fields',fieldID,'DateRange',oneDay,'OutputFormat','TimeTable','ReadKey',readAPIKey);
sumData=retime(data,'daily','sum');
bar(sumData.Timestamps,sumData.antSpray);
Though, I can't figure out how to work with 'datetime' to make like;
  • so far this week, per day
  • last week, per day
  • this month, per day
  • last month, per day
Christopher Stapels
Christopher Stapels 2020년 10월 13일
편집: Christopher Stapels 2020년 10월 13일
You coud use weekday
thisWeek = [datetime('today')-days(weekday(datetime('now'))) datetime('now')];
But a better method might be to create a derived channel. You can use the timecontrol to run weekly and save a summation to the derived channel. You can also save the monthly totals in the channel. You could have your script save daily values in field1, and weekly values in fiedl 2, etc.
Stigh Aarstein
Stigh Aarstein 2020년 10월 13일
Thanks Christopher,
Any idea how I can format the bar (x) to show hours rather than the current view?
I would like it to show 01 02 03 .... 20 21 22 23 (24 hour format).
Try
yAxisVals=0:23;
bar(yAxisVals,sumData.antSpray);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

도움말 센터File Exchange에서 Read Data from Channel에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by