4 Day temperature comparison, including current day

How can I include the current day in the 3 day temperature comparison.
[datetime('today')-days(0),datetime('today')], gives an error

댓글 수: 6

Hi Mark, in this array you only have current day twice. You should explain more for us to understand.
[datetime('today')-days(0),datetime('today')] gives an error
It doesn't for me. If you get an error, always give us the full text of the error message so we now what the problem is (for all we know you're using an old version of matlab without datetime).
And yes, as Omer said, give more details of what you want.
% Read temperature data from a ThingSpeak channel for three seperate days
% and visualize the data in a single plot using the MATLAB thingSpeakPlot
% function.
% Channel 12397 contains data from the MathWorks Weather Station, located
% in Natick, Massachusetts. The data is collected once every minute.
% Field 4 contains temperature data.
% Channel ID to read data from
readChannelID = 569794;
% Temperature Field ID
TemperatureFieldID = 1;
% Channel Read API Key
% If your channel is private, then enter the read API
% Key between the '' below:
readAPIKey = 'MXACLRJMFM18OYOR';
temperatureDay0 = thingSpeakRead(readChannelID,'Fields',[TemperatureFieldID], ...
'NumPoints',240, 'ReadKey',readAPIKey);
% Read Temperature Data. Learn more about the THINGSPEAKREAD function by
% going to the Documentation tab on the right side pane of this page.
temperatureDay0 = temperatureDay0(:, 1);
temperatureDay1 = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID, ...
'dateRange', [datetime('today')-days(1),datetime('today')], ...
'ReadKey',readAPIKey);
temperatureDay2 = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID, ...
'dateRange', [datetime('today')-days(2),datetime('today')-days(1)],...
'ReadKey',readAPIKey);
temperatureDay3 = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID, ...
'dateRange', [datetime('today')-days(3),datetime('today')-days(2)], ...
'ReadKey',readAPIKey);
% Create the array of temperatures
allData=[temperatureDay0(1:240),temperatureDay1(1:240),temperatureDay2(1:240),...
temperatureDay3(1:240)];
% Learn more about the THINGSPEAKPLOT function by going to the Documentation tab on
% the right side pane of this page.
thingSpeakPlot((1:240),allData,'legend',{'Day0','Day1','Day2','Day3'}, ...
'XLabel','Hour','YLabel','Temperature C', ...
'Title','3-Day Temperature Comparison');
Day 0 (current day) does not start at 00:00
I can get it to work with the past 3 days, but I would also like to add the current day.
Thank You for the replies.
'DateRange' value must have an end date and time later than the start date and time.
Error message when I use [datetime('today')-days(0),datetime('today')]
Is that the full text of the error message. Usually it also states which line(s) is responsible for the error. This is essential information we need, particularly since I cannot see anywhere in your code where you use [datetime('today')-days(0),datetime('today')].
On the other hand the error message is clear. The two dates you pass must be different, with the second one later than the first. Of course, subtracting 0 days from a date doesn't change that data, so [datetime('today')-days(0),datetime('today')] is twice the same date.
It's unclear what you expected to do with that.

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

 채택된 답변

Omer Yasin Birey
Omer Yasin Birey 2019년 2월 5일
편집: Omer Yasin Birey 2019년 2월 5일
You can try to use the last minute of the current day, since it says starting date must be before than ending date
todayDate = datetime('today');
todayDate = todayDate + minutes(1439);
[datetime('today')-days(0),todayDate]

댓글 수: 4

Subtracting 0 (days or anything) never does much, so I really don't see the point of datetime('today') - days(0). You may as well write datetime('today') and avoid puzzling the reader why you're subtracting nothing.
Assuming you're not running the code at exactly midnight
[datetime('today'), datetime]
will give you two different datetime on the same day. No idea if that's what desired.
Well actually, datetime('today') and datetime('today')-days(0) does differ. As far as I can understand OP wanted to display dates with the format of 'dd,MMMM, yyyy HH:mm:ss' and on this matter to show hours, we must write
datetime('today','Format','dd,MMMM, yyyy HH:mm:ss')
Firstly, it is a longer line and to be honest I wouldn't want that in my array it can be quite confusing. Secondly, it takes more time to process. While this line is taking 0.017166 seconds, days(0) version takes only 0.003238 seconds. It is over 5 times faster.
Well ok, it does create a datetime with a different display format. If that's the whole purpose of subtracting 0, then I would say it's a bad idea. First, you're relying on undocumented behaviour, so that may change in a future release without notice. Secondly, if you want a specific format you'd be better be explicit about the format you want. So even though, 'Format', 'dd, MMM, yyyy HH:mm:ss' might be longer, it's at least very clear what the intent of the code is. Subtracting 0 just to alter the format, with no comment explaining what the purpose is, is just asking for a bug to be introduced later on when a different maintainer notice that 0 subtraction and remove it.
In this particular case, where the array is just passed to another function, the format does not matter at all.
I echo Guillaume's comment.
However, just for the record, it is typically faster to subtract a pure number rather than days(number). For example,
datetime('today')-1
is faster than
datetime('today')-days(1)
and yet has the same meaning/result. Just note that days(1) might be more readable.

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

추가 답변 (3개)

KSSV
KSSV 2019년 2월 5일
thedates = (datetime('today')-days(3):datetime('today'))'
Mark Clark
Mark Clark 2019년 2월 6일

0 개 추천

This is what Im trying to do. The today trace in the graph below.
CaptureAGT.PNG
Mark Clark
Mark Clark 2019년 2월 6일

0 개 추천

See https://thingspeak.com/channels/569794

커뮤니티

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

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

질문:

2019년 2월 5일

편집:

2019년 2월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by