Hans on IoT

ThingSpeak, MATLAB, and the Internet of Things

Updates to the MATLAB Analysis App with Lots of Example Code 3

Posted by Hans Scharler,

When using the MATLAB Analysis app on ThingSpeak, the MATLAB function to represent date and time (datetime) allows you to represent points in time. You can also use datetime(‘now’)datetime(‘today’), datetime(‘yesterday’), or datetime(‘tomorrow’) to create scalar datetimes at or around the current moment. Check out our documentation for more information about the datetime function.

On ThingSpeak, so far, the datetime function returned time set to UTC time zone by default. Starting at 10 am (EDT) on September 10th 2015, the datetime function will return date and time set to your account time zone (at https://thingspeak.com/account). This will allow you to read data from your channel with timestamps zoned to your local time zone instead of UTC.

For example, my account time zone is set to Eastern Time (US & Canada), and when I ran the following MATLAB code at 12:23 pm, I received:

dt = datetime('now')
dt =
     10-Sep-2015 12:23:35

Prior to this change, I would have received:

dt =
     10-Sep-2015 16:23:35

As you can see, the timestamp is 4 hours ahead of my time zone, which was due to MATLAB returning time in UTC.

This change makes it easier for you to perform time related activities in your time zone. Note that this new feature is available for both thingSpeakRead and thingSpeakWrite functions as well. As an example, consider the following request to read data from the MathWorks Weather Channel:


[data, timestamp] = thingSpeakRead(12397);
display(timestamp.TimeZone, 'TimeZone');


data =
     225.0000    3.8000   43.9000   95.8000
     0   29.9800    4.3000    0.0300
timestamp =
     10-Sep-2015 16:13:54
TimeZone =

With this enhancement, you would no longer have to explicitly specify the time zone of your dates and time to read and write data in your time zone.

Here are a few other examples:

  1. Read data corresponding to one entire day in your timezone:
startDateTime = datetime('September 10, 2015 00:00:00')
endDateTime = datetime('September 10, 2015 23:59:59')
readChannelID = 12397;
[data, timeStamps] = thingSpeakRead(readChannelID, 'DateRange', [startDateTime, endDateTime])
  1. Read data between certain hours of a day (between 7 am and 9 pm):
startDateTime = datetime('September 10, 2015 07:00:00')
endDateTime = datetime('September 10, 2015 21:00:00')
readChannelID = 12397;
[data, timeStamps] = thingSpeakRead(readChannelID, 'DateRange', [startDateTime, endDateTime])
  1. Generate a MATLAB plot in your local time zone:
[data, timeStamps] = thingSpeakRead(12397, 'Fields', 3, 'NumPoints', 10);
plot(timeStamps, data)

Note that, if at present, you are explicitly setting the time zone to your local time zone, you might see unexpected behavior in your code. Here are a few examples, based on support requests we have received:

  1. If you are using datetime function in your code similar to the example below:
% Set the time now to variable dt
dt = datetime('now')
% Assign time zone to UTC since the dt is unzoned by default
dt.TimeZone = 'UTC';
% Convert the timestamp to ‘America/New_York’
dt.TimeZone = 'America/New_York'

To fix this, remove the “TimeZone” assignments since time is now returned in your time zone by default, and use the code below:

% Set the time now to variable dt
dt = datetime('now')
  1. If you are setting the time zone of data returned by thingSpeakRead to your zone:
% Read data from a channel
[data, timeStamps] = thingSpeakRead(12397);
% Set the timezone to match your zone
timeStamps.TimeZone = 'America/New_York';

To fix this, remove the line with the “TimeZone” assignment, and use the code below:

% Read data from a channel
[data, timeStamps] = thingSpeakRead(12397);

For more information about the datetime function refer to the MATLAB documentation. If you need support, use the MATLAB section of the ThingSpeak Forum.

3 CommentsOldest to Newest

paul reisinger replied on : 1 of 3
Hello I just started using Thingspeak a few days ago. In My Profile, time zone is set to : Eastern Time/Us & Canada. This works fine in the Charts view which show the correct local time on the x axis, But when I use the Export function the resulting csv file has UTC timeStamps. So what do I need to do to get the csv file with my local time timestamps? Thanks, Paul
Claude Andrew replied on : 2 of 3
I am using a MATLAB Visualization and what I've found is the exact opposite of what you say in this post for thingSpeakRead. The data is returned in UTC time but with no TimeZone in the datetime. If I make no adjustment to the TimeZone then the MATLAB Visualization plots the data with UTC time while the ThingSpeak charts of the same data on the same page *are* adjusted to my account's timezone. To adjust to my local time I have to first set the TimeZone to 'Etc/UTC' and then set to my local timezone thus: times.TimeZone='Etc/UTC'; times.TimeZone='America/Los_Angeles'; Perhaps things have moved on since you wrote this article but, if I haven't made some mistake in my understanding, it might be worth updating.
Hans Scharler replied on : 3 of 3
You are correct. ThingSpeak does adjust its embedded plots to the viewer's timezone. The MATLAB Plots do not. They use UTC as the default, but as you pointed out, the timezone can be adjusted. Thanks.