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 ____________________ __________________________ ____________ 15-Jan-2019 08:16:15 129 26.1 15-Jan-2019 08:17:16 115 26.1 15-Jan-2019 08:18:19 133 26.2
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')
Retrieve the most recent result for all fields of a public channel, including the timestamps.
[data,timestamps] = thingSpeakRead(12397)
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)
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)])
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.
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.
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.
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.
You have a modified version of this example. Do you want to open this example with your edits?