ThingSpeak: How to set 'NumPoints' equal to the maximum available in the channel?
11 views (last 30 days)
Show older comments
Luke Martin on 16 Nov 2021
I'm looking for a way to set the '2733' in my command below equal to the amount of datapoints available.
[data, time] = thingSpeakRead(readChannelID, 'Field', fieldID1, 'NumPoints', 2733, 'ReadKey', readAPIKey);
For context, I'm trying to setup visualisations that still work when the dataset in the channel is changed. For my current dataset, I have 2733 lines so the above works, but I'd like to get it to the point where it can work without amendment when I change the dataset.
Vinod on 16 Nov 2021
See this documentation. The NumPoints can be at max 8000. If you have less than 8000 points in your channel, everything is returned. If you have >8000 points, only the most recent 8000 are returned.
I'd recommend also answering this survey from the developers as they are looking to provide functionality based on use cases.
More Answers (1)
Rogier on 13 Jul 2022
I have written this bit of code to help with this problem for myself, figured I'd share
% Channel to read from
readChannelID = 123456789;
% Set these to start & end of the data you're interested in
startDate = datetime('2022-07-06');
endDate = datetime('now');
% How often data is logged, max. This example is once per minute.
% If you don't know exactly, make sure this is higher. So if it
% is roughly once a minute, but can sometimes be twice a minute for
% a bit, be safe and set to '2' and 'minute' for twice a minute.
frequencyTimes = 1;
frequencyPer = 'minute'; % can be 'year', 'month', 'day', 'hour', 'minute', 'second', or 'millisecond'
% Example of output data
total = 0;
% Leave this block alone
rangeEndDateNum = addtodate(datenum(startDate), 8000 / frequencyTimes, frequencyPer);
rangeEndDate = datetime(rangeEndDateNum, 'ConvertFrom', 'datenum');
dateRange = [startDate, rangeEndDate];
diff = rangeEndDate - startDate;
while dateRange(1) < endDate
if dateRange(2) > endDate
dateRange(2) = endDate;
% Retrieve data
[data, time, info] = thingSpeakRead(readChannelID, DateRange=dateRange);
% Do something with data
numberOfElements = numel(time);
% Accumulate with previous data
total = total + numberOfElements;
% This sets up the next chunk
dateRange = dateRange + diff;
% Do something with the accumulated data
display(total, 'Total number of datapoints in ThingSpeak channel')
More Answers in the ThingSpeak Community
Find more on Visualize Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!