Read data stored in a ThingSpeak channel
reads the most recent data from all fields of the specified public channel on ThingSpeak.com and returns the data as a
numeric type.data
= thingSpeakRead(channelID
)
uses additional options specified by one or more
data
= thingSpeakRead(channelID
,Name,Value
)Name
,Value
pair arguments. To read nonnumeric
data, you must specify the ‘OutputFormat
’.
[
also returns timestamps from the specified channel on ThingSpeak.com and can include any of the
input arguments in previous syntaxes.data
,timestamps
]
= thingSpeakRead(___)
[
also returns channel information.data
,timestamps
,channelInfo
]
= thingSpeakRead(___)
Retrieve the last five minutes of data from fields 1 and 4 of a public channel, and return the data in a timetable.
data = thingSpeakRead(12397,'Fields',[1,4],'NumPoints',3,'OutputFormat','TimeTable')
data = 3×2 timetable Timestamps WindDirectionNorth0Degrees TemperatureF ____________________ __________________________ ____________ 14-May-2020 10:17:34 175 64 14-May-2020 10:18:34 223 64.3 14-May-2020 10:19:34 211 64.1
Read the last five minutes of data from fields 1 and 4 of a public channel, and return the data in a table.
data = thingSpeakRead(12397,'Fields',[1,4],'Numminutes',5,'OutputFormat','TimeTable')
data = 11×2 timetable Timestamps WindDirectionNorth0Degrees TemperatureF ____________________ __________________________ ____________ 14-May-2020 10:08:34 216 63.2 14-May-2020 10:09:34 183 62.9 14-May-2020 10:10:34 139 63 14-May-2020 10:11:34 179 62.9 14-May-2020 10:12:34 225 62.9 14-May-2020 10:13:34 250 63 14-May-2020 10:14:34 177 63.2 14-May-2020 10:15:34 196 63.8 14-May-2020 10:16:34 196 63.8 14-May-2020 10:17:34 175 64 14-May-2020 10:18:34 223 64.3
Retrieve the most recent result for all fields of a public channel, including the timestamp.
[data,timestamps] = thingSpeakRead(12397)
data = 199.0000 6.1000 18.0000 63.6000 0 30.0500 4.1390 0 timestamps = datetime 14-May-2020 10:20:34
Retrieve the last five minutes of data from fields 1 and 4 of a public channel, including the timestamp and channel information.
[data,timestamps,channelInfo] = thingSpeakRead(12397,'Fields',[1,4],'NumMinutes',5)
data = 316.0000 62.5000 276.0000 62.9000 124.0000 63.0000 283.0000 63.6000 94.0000 63.8000 285.0000 64.1000 233.0000 64.1000 269.0000 64.0000 255.0000 63.8000 203.0000 64.0000 237.0000 64.1000 timestamps = 11×1 datetime array 14-May-2020 10:24:34 14-May-2020 10:25:34 14-May-2020 10:26:34 14-May-2020 10:27:34 14-May-2020 10:28:34 14-May-2020 10:29:34 14-May-2020 10:30:34 14-May-2020 10:31:34 14-May-2020 10:32:34 14-May-2020 10:33:34 14-May-2020 10:34:34 channelInfo = struct with fields: ChannelID: 12397 Name: 'WeatherStation' Description: 'MathWorks Weather Station, West Garage, Natick, MA 01760, USA' Latitude: 42.2997 Longitude: -71.3505 Altitude: 60 Created: 20-May-2014 17:50:32 Updated: 14-May-2020 10:28:40 LastEntryID: 2852997 FieldDescriptions: {'Wind Direction (North = 0 degrees)' 'Wind Speed (mph)' '% Humidity' 'Temperature (F)' 'Rain (Inches/minute)' 'Pressure ("Hg)' 'Power Level (V)' 'Light Intensity'} FieldIDs: [1 2 3 4 5 6 7 8] URL: 'https://api.thingspeak.com/channels/12397/feed.json?'
Retrieve the data from 11:59:01 on August 9, 2018 through 12:02:52 on August 10, 2018 for fields 1 and 4 of a public channel, including the timestamp and channel information.
[data,timestamps,channelInfo] = thingSpeakRead(12397,'Fields',[1 4],... 'DateRange',[datetime(2018,8,9,23,59,01),datetime(2018,8,10,0,02,52)])
data = 202.0000 83.3000 184.0000 83.2000 185.0000 83.1000 155.0000 83.1000 timestamps = 4×1 datetime array 09-Aug-2018 23:59:17 10-Aug-2018 00:00:20 10-Aug-2018 00:01:21 10-Aug-2018 00:02:22 channelInfo = struct with fields: ChannelID: 12397 Name: 'WeatherStation' Description: 'MathWorks Weather Station, West Garage, Natick, MA 01760, USA' Latitude: 42.2997 Longitude: -71.3505 Altitude: 60 Created: 20-May-2014 17:50:32 Updated: 14-May-2020 10:31:40 LastEntryID: 2853000 FieldDescriptions: {'Wind Direction (North = 0 degrees)' 'Wind Speed (mph)' '% Humidity' 'Temperature (F)' 'Rain (Inches/minute)' 'Pressure ("Hg)' 'Power Level (V)' 'Light Intensity'} FieldIDs: [1 2 3 4 5 6 7 8] URL: 'https://api.thingspeak.com/channels/12397/feed.json?'
channelID
— Channel identification numberThe channel identification number, specified as a positive integer. For private
channels, you must also specify the ‘ReadKey
’ argument.
Data Types: double
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
thingSpeakRead(12397,'Location',true);
'DateRange'
— Range of data to returnRange of data to return, specified as a comma-separated pair consisting of
'DateRange'
and an array of values that have
[startdate,enddate]
in MATLAB®
datetime
values. ThingSpeak server limits the number of points returned to a maximum of 8000. Adjust
your ranges or make multiple calls if you need more than 8000 points of data.
Note
You cannot use DateRange
with NumDays
or
NumMinutes
.
Example: thingSpeakRead(12397,'DateRange',[datetime('Aug 8,
2014'),datetime('Aug 12, 2014')]);
'Fields'
— Channel fieldsChannel field IDs, specified as a comma-separated pair consisting
of 'Fields'
and numeric values of the channel field
ID to retrieve data from.
Example: thingSpeakRead(12397,'Fields',[1,3,4]);
'Location'
— Positional information of dataIndicator to display positional information of data from the channel, specified as a
comma-separated pair consisting of 'Location'
and a logical value.
Location information includes latitude, longitude, and altitude.
Example: thingSpeakRead(12397,'Location',true);
'NumDays'
— Number of days of dataNumber of days of data to extract data from, specified as a
comma-separated pair consisting of 'NumDays'
and
a numeric value. Number of days is measured in 24-hour periods. The ThingSpeak server
limits the number of points returned to a maximum of 8000. Adjust
your number of days, or make multiple calls if you need more than
8000 points of data.
Note
You cannot use NumDays
with DateRange
or NumMinutes
.
Example: thingSpeakRead(12397,'NumDays',2);
'NumMinutes'
— Number of minutes of dataNumber of minutes of data to extract data from, specified as
a comma-separated pair consisting of 'NumMinutes'
and
a numeric value. Number of minutes of data to retrieve is measured
from the time the command is issued. The ThingSpeak server limits
the number of points returned to a maximum of 8000. Adjust your number
of minutes, or make multiple calls if you need more than 8000 points
of data.
Note
You cannot use NumMinutes
with DateRange
or NumDays
.
Example: thingSpeakRead(12397,'NumMinutes',20);
Data Types: double
'NumPoints'
— Number of data pointsNumber of data points to extract, specified as a comma-separated
pair consisting of 'NumPoints'
and a numeric value.
The number of points or rows of data is counted from the time the
command is issued. The ThingSpeak server limits the number of
points returned to a maximum of 8000.
Example: thingSpeakRead(12397,'NumPoints',200);
'OutputFormat'
— Class of the output dataClass of the output data, specified as a comma-separated pair
consisting of 'OutputFormat'
and a text value.
Valid values are 'matrix'
, 'table'
,
and 'timetable'
. Use 'matrix'
format
for numeric data. You can use 'timetable'
or 'table'
for
any nonnumeric data. The 'table'
format outputs [data,channelInfo]
,
where data
contains the timestamps and the data
from the fields of the channel. The 'timetable'
format
outputs [data,channelInfo]
, where data
is
a timetable and contains as many variables as the number of requested 'fields'
.
Example: thingSpeakRead(12397,'OutputFormat','table');
Example: thingSpeakRead(12397,'OutputFormat','timetable');
'ReadKey'
— Read API key of channelRead API key of the channel, specified as a comma-separated pair consisting of
'ReadKey'
and a character vector representing a channel read API
key. The Read API key allows you to read data from a private channel. You can find the
Read API key for a channel on the API Keys tab of your ThingSpeak channel view. If you
are reading data from a public channel, you do not need a Read API key. Save your
channel Read API key in a variable for convenience.
Example: thingSpeakRead(12397,'ReadKey','F6CSCVKX42WFZN9Y');
Data Types: char
'Timeout'
— Server connection timeout periodServer connection timeout period, specified as a comma-separated
pair consisting of 'Timeout'
and a numeric value.
The value is the number of seconds allowed for thingSpeakRead
to
connect to the server.
Example: thingSpeakRead(12397,'Timeout',15);
data
— Data from the channelData from the channel, returned as an array, table, or timetable.
timestamps
— Timestamp of each data elementTimestamp of each data element, returned as an array of datetime
values.
Data Types: datetime
channelInfo
— Channel informationChannel information, returned as a structure with fields in the order shown in the table..
Field | Description |
---|---|
ChannelID | Channel identification number |
Name | Name of the channel |
Description | Channel description |
Latitude | Latitude for the channel as provided in the channel settings |
Longitude | Longitude for the channel as provided in the channel settings |
Altitude | Elevation for the channel as provided in the channel settings |
Created | Datetime of channel creation date |
Updated | Datetime when the channels settings were last changed |
Last EntryID | Entry ID for the most recent channel update |
Field Descriptions | List of field names |
Field IDs | List of enabled fields |
URL | URL for feed data |
ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live data streams in the cloud. See License Options and the Commercial Use for more information.