4:06 am

Silver

March 13, 2019

as I'm using MATLAB more and more, I tried to visualize how many weeks my sensors can run on rechargable batteries. As my hardware doesn't cut off the battery when it's drained I might risk my LiFePO4's.

So I wanted the possibility to easily distinguish between data for one week and the other - but my coding is, well, suboptimal and also not really maintainable:

My goal was something like

(1) calculate the data for 2 or 3 months

(2) calculate both, absolute miminum as well as maximum

(3) plot the data in respect to min/max with each week showing a differing color

And this is how I did it:

voltageWeek1 = thingSpeakRead(readChannelID,'Fields',voltageFieldID,'dateRange',...

[datetime('now')-days(7),datetime('now')],'ReadKey',readAPIKey,...

'Outputformat','Timetable');

voltageWeek2 = thingSpeakRead(readChannelID,'Fields',voltageFieldID,'dateRange',...

[datetime('now')-days(14),datetime('now')-days(7)],'ReadKey',readAPIKey,...

'Outputformat','Timetable');

voltageWeek3 = thingSpeakRead(readChannelID,'Fields',voltageFieldID,'dateRange',...

[datetime('now')-days(21),datetime('now')-days(14)],'ReadKey',readAPIKey,...

'Outputformat','Timetable');

voltageWeek4 = thingSpeakRead(readChannelID,'Fields',voltageFieldID,'dateRange',...

[datetime('now')-days(28),datetime('now')-days(21)],'ReadKey',readAPIKey,...

'Outputformat','Timetable');

voltageWeek5 = thingSpeakRead(readChannelID,'Fields',voltageFieldID,'dateRange',...

[datetime('now')-days(35),datetime('now')-days(28)],'ReadKey',readAPIKey,...

'Outputformat','Timetable');

voltageWeek6 = thingSpeakRead(readChannelID,'Fields',voltageFieldID,'dateRange',...

[datetime('now')-days(42),datetime('now')-days(35)],'ReadKey',readAPIKey,...

'Outputformat','Timetable');

voltageWeek7 = thingSpeakRead(readChannelID,'Fields',voltageFieldID,'dateRange',...

[datetime('now')-days(49),datetime('now')-days(42)],'ReadKey',readAPIKey,...

'Outputformat','Timetable');

voltageWeek8 = thingSpeakRead(readChannelID,'Fields',voltageFieldID,'dateRange',...

[datetime('now')-days(56),datetime('now')-days(49)],'ReadKey',readAPIKey,...

'Outputformat','Timetable');

It does what it is meant for but I beg there's a much more sophisticated solution which not only will save characters but also calculation time. But how?

Another question is related to the same topic:

I need to detect the minimum and maximum for all weeks.

minimumW1= min(voltageWeek1.v);

minimumW2= min(voltageWeek2.v);

minimumW3= min(voltageWeek3.v);

minimumW4= min(voltageWeek4.v);

minimumW5= min(voltageWeek5.v);

minimumW6= min(voltageWeek6.v);

minimumW7= min(voltageWeek7.v);

minimumW8= min(voltageWeek8.v);

minimumW9= min(voltageWeek9.v);

minimumW10= min(voltageWeek10.v);

minimumW11= min(voltageWeek11.v);

minimumW12= min(voltageWeek12.v);

minimumW= min([minimumW1, minimumW2, minimumW3, minimumW4, ...

minimumW5, minimumW6, minimumW7, minimumW8, ...

minimumW9, minimumW10, minimumW11, minimumW12]);

Same for maximum. Again, not very intelligent. How should I alter my codings?

The plot was the initial reason to do it like I did:

hold on;

plot(voltageWeek1.Timestamps, voltageWeek1.v);

plot(voltageWeek2.Timestamps, voltageWeek2.v);

plot(voltageWeek3.Timestamps, voltageWeek3.v);

plot(voltageWeek4.Timestamps, voltageWeek4.v);

plot(voltageWeek5.Timestamps, voltageWeek5.v);

plot(voltageWeek6.Timestamps, voltageWeek6.v);

plot(voltageWeek7.Timestamps, voltageWeek7.v);

plot(voltageWeek8.Timestamps, voltageWeek8.v);

plot(voltageWeek9.Timestamps, voltageWeek9.v);

plot(voltageWeek10.Timestamps, voltageWeek10.v);

plot(voltageWeek11.Timestamps, voltageWeek11.v);

plot(voltageWeek12.Timestamps, voltageWeek12.v);

hold off;

So one issue let to another... while the date should be printed on the x-axis.

Thanks your help

Most Users Ever Online: 166

Currently Online:

15 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:

anis, 43994554AKS, suda, marisol, qkpham, SANGAMNATH BModerators: cstapels: 460

Administrators: Hans: 405, lee: 457