Calculating averages per weekday | 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
Calculating averages per weekday
No permission to create posts
January 5, 2017
4:34 am
Avatar
nielsb100
New Member
Members
Forum Posts: 2
Member Since:
January 5, 2017
sp_UserOfflineSmall Offline

 Hi,

 

I am logging my energy usage to channel 203631 (public).

I would like to calculate the average (or median) energy usage per weekday (monday/tuesday/wednesday/....).  I am clueless on how to do this. Can somebody help me?

The channel contains 2 fields, no1 = the energy used per 30 seconds. The other the sum of energy used per 24 hours. I hope the timestamp from the channel it self can be used for date/time that is requierd.

 

Feel free to use the data from the channel.

 

Thanx in advance!

 

Niels

January 5, 2017
7:13 pm
Avatar
piajola

Gold
Members
Forum Posts: 87
Member Since:
October 12, 2015
sp_UserOfflineSmall Offline

Hi Niels,

Please first run as is (hope it run in your machine 😉
See the output windows to check it is doing something useful ...
Read the comments ( % ) then Change it however you want/need

Finally post questions

How are you reading watts? Can we know? Always good to know Wink

 

ver
[ 'Time is=> UTC ' datestr(datetime('now','TimeZone','UTC')) ' - Lima ' datestr(datetime('now','TimeZone','America/Lima'))]
%
%
diChanAiDi = 203631;
%
taim = 1;    % TimeStamps
cam1 = 2;   % Field1
cam2 = 3;   % Field2
% etc
%
% t1=datetime('now');    % time at running time i.e. "right now"

yi=2017;   % year(t1); initial point in space-time -- as in Star Trek 😉
mi=1;      % month(t1);
di=5;       % day(t1);
hi=16;     % hour(t1);
mmi=0;   % minute(t1);
si=0;       % second(t1);

yf=2017;  %ending point in desired time lapse
mf=1;
df=5;
hf=16;
mmf=14; % minute(t1);
sf=59;     % second(t1);

%tini=datetime([yi,mi,di,hi,mmi,si],'TimeZone','UTC')
tini=datetime([yi,mi,di,hi,mmi,si]);
tfin=datetime([yf,mf,df,hf,mmf,sf]);

tkomo = 0;
if tkomo==1
tini.TimeZone = 'UTC';
tfin.TimeZone = 'UTC';
else
tini.TimeZone = 'America/Lima';     % change as per your galaxy and sun color
tfin.TimeZone = 'America/Lima';
end

%tini.TimeZone, tfin.TimeZone
['from-> ' datestr(tini) ' ** to-> ' datestr(tfin)]

%[datCH] = thingSpeakRead(diChanAiDi,'Fields',[1 2],'numpoints',100,'OutputFormat','table'); % last 100 points
[datCH] = thingSpeakRead(diChanAiDi,'Fields',[1 2],'DateRange',[tini,tfin],'OutputFormat','table')

t1 = datCH{:, taim};   %
x1 = datCH{:, cam1}; % datCH{start:end, field} nothing:nothing means ALL array
x2 = datCH{:, cam2}; %

['total kw-> ' num2str(sum(x1))]

%second(t1)

% prome=mean(x1);
['median/avrg kw-> ' num2str(median(x1))]
% prome=max(x1);
% prome=min(x1);

xdif = diff(datCH{:, taim}) % keyword diff finds 2nd-1st, 3rd-2nd, 4th-3rd, 5th-4th,.. so on to end of array
['there are not regular 30 sec.']

January 8, 2017
3:38 pm
Avatar
nielsb100
New Member
Members
Forum Posts: 2
Member Since:
January 5, 2017
sp_UserOfflineSmall Offline

Thanx piajola!

I have been working on the code and have so far the following code.

Now i want to calculate the averages per weekday. haven't figured out how to do this.

 

----

 

ver
[ 'Time is=> UTC ' datestr(datetime('now','TimeZone','UTC')) ' - Lima ' datestr(datetime('now','TimeZone','America/Lima'))]
%
%
diChanAiDi = 203631;
%
taim = 1;    % TimeStamps
cam1 = 2;   % Field1
cam2 = 3;   % Field2
% etc
%
% t1=datetime('now');    % time at running time i.e. "right now"

yi=2017;   % year(t1); initial point in space-time -- as in Star Trek
mi=1;      % month(t1);
di=5;       % day(t1);
hi=0;     % hour(t1);
mmi=0;   % minute(t1);
si=0;       % second(t1);

yf=2017;  %ending point in desired time lapse
mf=1;
df=5;
hf=23;
mmf=59;    % minute(t1);
sf=59;     % second(t1);

for j=1:8,    %day x to y
    
%stepsize to 7? for weekdays
    
%assign j to any of the yi - di variables
di = j;
df = j;

%tini=datetime([yi,mi,di,hi,mmi,si],'TimeZone','UTC')
tini=datetime([yi,mi,di,hi,mmi,si]);
tfin=datetime([yf,mf,df,hf,mmf,sf]);

tkomo = 0;
if tkomo==1
tini.TimeZone = 'UTC';
tfin.TimeZone = 'UTC';
else
tini.TimeZone = 'America/Lima';     % change as per your galaxy and sun color
tfin.TimeZone = 'America/Lima';
end

%tini.TimeZone, tfin.TimeZone

%[datCH] = thingSpeakRead(diChanAiDi,'Fields',[1 2],'numpoints',100,'OutputFormat','table'); % last 100 points

datCH = thingSpeakRead(diChanAiDi,'Fields',[1 2],'DateRange',[tini,tfin],'OutputFormat','table');

t1 = datCH{:, taim};   %
x1 = datCH{:, cam1};  % datCH{start:end, field} nothing:nothing means ALL array
x2 = datCH{:, cam2};  %

out =['total energy during ' num2str(yi) '-' num2str(mi) '-' num2str(di) ': ' num2str(max(x2)) 'Kwh'];
disp(out)

end

%to figure out: group by weekday and post to anonther channel

January 9, 2017
12:25 pm
Avatar
Rob Purser
Natick, MA

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

I'm probably being simplistic, but timetables in MATLAB are probably what you need here.  You can use the retime function to apply an operation on a daily basis:

energyData = thingSpeakRead(203631, 'NumDays', 7, 'OutputFormat', 'timetable');
retime(energyData,'daily',@median)

 

You'll get:

ans =

5×2 timetable array

Timestamps Kwatt TotalKwh
____________________ _____ ________

05-Jan-2017 00:00:00 0.37 8.642
06-Jan-2017 00:00:00 0.36 5.373
07-Jan-2017 00:00:00 0.37 7.736
08-Jan-2017 00:00:00 0.24 5.787
09-Jan-2017 00:00:00 0.23 3.357

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 8, 2017
5:18 am
Avatar
rayellam

Silver
Members
Forum Posts: 11
Member Since:
April 2, 2015
sp_UserOfflineSmall Offline

Neat Rob, just what im looking for as a starting point for my application (i think)

 

Thanks

Ray

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
18 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:

celinabw2, FitoSprayscroma, LunnonGor, lancevl16, bernicenk60, veronicadq18

Administrators: Hans: 387, lee: 457