This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

thingSpeakRead

Read data stored in a ThingSpeak channel

Syntax

data = thingSpeakRead(channelID)
data = thingSpeakRead(channelID,Name,Value)
data = thingSpeakRead(___,'ReadKey','channel Read API key')
[data,timestamps] = thingSpeakRead(___)
[data,timestamps,channelInfo] = thingSpeakRead(___)

Description

example

data = thingSpeakRead(channelID) reads the most recent data from all fields of the specified public channel on ThingSpeak.com and returns the data as a numeric type.

example

data = thingSpeakRead(channelID,Name,Value) uses additional options specified by one or more Name,Value pair arguments. To read nonnumeric data, you must specify the ‘OutputFormat’.

example

data = thingSpeakRead(___,'ReadKey','channel Read API key') uses the ThingSpeak™ Read API key to read from a private channel.

example

[data,timestamps] = thingSpeakRead(___) also returns timestamps from the specified channel on ThingSpeak.com and can include any of the input arguments in previous syntaxes.

example

[data,timestamps,channelInfo] = thingSpeakRead(___) also returns channel information.

Examples

collapse all

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')
data =

  5×2 timetable

         Timestamps         WindDirectionNorth0Degrees    TemperatureF
    ____________________    __________________________    ____________

    13-Nov-2018 09:22:48               117                    53.4    
    13-Nov-2018 09:23:48               110                    53.4    
    13-Nov-2018 09:24:50                90                    53.5    
    13-Nov-2018 09:25:52                95                    53.5    
    13-Nov-2018 09:26:55                90                    53.5    

Retrieve the most recent result for all fields of a public channel, including the timestamps.

[data,timestamps] = thingSpeakRead(12397)
data =

  Columns 1 through 7

  175.0000    7.5000   45.0000   47.5000         0   29.6000    3.4000

  Column 8

   39.0000


timestamps = 

  datetime

   03-Jan-2019 14:30:36

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 =

  315.0000   39.7000
  314.0000   39.7000
  314.0000   39.8000
  314.0000   39.9000
  315.0000   39.9000


timestamps = 

  5×1 datetime array

   20-Nov-2018 08:29:39
   20-Nov-2018 08:30:40
   20-Nov-2018 08:31:41
   20-Nov-2018 08:32:41
   20-Nov-2018 08:33:43


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: 25-Sep-2018 20:08:42
          LastEntryID: 2132316
    FieldDescriptions: {1×8 cell}
             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: 25-Sep-2018 20:08:42
          LastEntryID: 2122785
    FieldDescriptions: {1×8 cell}
             FieldIDs: [1 2 3 4 5 6 7 8]
                  URL: 'https://api.thingspeak.com/channels/12397/feed.json?'

Input Arguments

collapse all

The channel identification number, specified as a positive integer. For private channels, you must also specify the ‘ReadKey’ argument.

Data Types: double

Name-Value Pair Arguments

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.

Example: thingSpeakRead(12397,'Location',true);

Range 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')]);

Channel 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]);

Indicator 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);

Number 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);

Number 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

Number 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);

Class 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');

Read API key of the channel, specified as a comma-separated pair consisting of 'ReadKey' and a character vector representing the channel read API key. The Read API key allows you to read data from a private channel. 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

Server 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);

Output Arguments

collapse all

Data from the channel, returned as an array, table, or timetable.

Timestamp of each data element, returned as an array of datetime values.

Data Types: datetime

Channel information, returned as a structure with fields in the order shown in the table..

FieldDescription
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

More About

collapse all

ThingSpeak

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.

See Also

Functions

Introduced in R2019a