Time Series Plot X-Axis Values Wrong Timezone - help please!

3 views (last 30 days)
When I try to plot temperature data over time the displayed time is two hours behind.
When I try to change timezones in the following manner results are very erratic:
[data, time] = thingSpeakRead(readChannelID, 'Field', fieldID1, 'NumPoints', 64, 'ReadKey', readAPIKey);
%% Visualize Data %%
time.TimeZone = 'Europe/Brussels'
plot(time, data,'- . r');
Most timezones from this list don't change the time at all, using using some I get say one hour closer but never the right amount.
What am I doing wrong? How come only a few timezones from the list even have any effect? Keep in mind I am a n00b.
For the record I am using the in browser editor on thingspeak.com, running a MATLAB visualisation plotting my cloud data.

Answers (1)

Benjamin Kraus
Benjamin Kraus on 17 Mar 2023
I think the problem you are having is that the data, when read from ThingSpeak, creates a datetime object with no TimeZone specified. When you first set the TimeZone (it goes from blank to not blank), MATLAB assumes the original data was in that new TimeZone, so the values displayed will not change. If you subsequently change the time zone, it will convert from the original time zone to the new time zone.
For example:
% Create a datetime without specifying the time zone.
d = datetime(2023,3,17,12,0,0); % Noon on March 17.
% The datetime has no TimeZone by default.
d.TimeZone
ans = 0×0 empty char array
% If you set the TimeZone to 'Europe/Brussels', it will assume the original
% time was in that time zone, and nothing will change.
d.TimeZone = 'Europe/Brussels'
d = datetime
17-Mar-2023 12:00:00
% Now if you change the time zone, it will automatically translate to the
% new location.
d.TimeZone = 'America/New_York'
d = datetime
17-Mar-2023 07:00:00
% But if you start with the original datetime again, and change it directly
% to 'America/New_York', you will get a different answer.
d = datetime(2023,3,17,12,0,0);
d.TimeZone
ans = 0×0 empty char array
d.TimeZone = 'America/New_York'
d = datetime
17-Mar-2023 12:00:00
d.TimeZone = 'Europe/Brussels'
d = datetime
17-Mar-2023 17:00:00
In your example code above, this is what you need to change:
[data, time] = thingSpeakRead(readChannelID, 'Field', fieldID1, 'NumPoints', 64, 'ReadKey', readAPIKey);
time.TimeZone = originalDataTimeZone; % You need to figure out the original time zone of the data.
time.TimeZone = 'Europe/Brussels'
plot(time, data,'- . r');

Communities

More Answers in the  ThingSpeak Community

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!