Error : Number of elements in 'Timestamps' value must be equal to the number of rows of 'Values' value.

5 views (last 30 days)
I'm coming across this error when I try to execute my code and I'm getting a little bit confused. The purpose of the code is to take data from a particular field from a channel in Thingspeak, analyze it and post the analyzed data to a particular field in another channel. The error is:
Number of elements in 'Timestamps' value must be equal to the number of rows of 'Values' value.
data = thingSpeakRead(ChannelID,'ReadKey',readAPIKey,'Fields',4,'DateRange',[datetime(2019,7,6,14,00,00),datetime('now')]);
%% Analyze Data %%
% Add code in this section to analyze data and store the result in the
% 'analyzedData' variable.
analyzedData = data;
%% Write Data %%
thingSpeakWrite (ChannelID,analyzedData,'WriteKey',writeAPIKey,'Fields',1,'Timestamps',datetime('now'));

Answers (1)

Mario Chiappelli
Mario Chiappelli on 24 Jul 2019
Edited: Mario Chiappelli on 25 Jul 2019
I would assume the issue is that you are reading in (for example) 10 data points that match up with 10 date times. You then are writing the 10 analyzed points to thingspeak but you only have one date time that it matches up with.
If you want to store the date times of the incoming points, try something like this:
[data,timestamps] = thingSpeakRead(ChannelID,'ReadKey',readAPIKey,'Fields',4,'DateRange',[datetime(2019,7,6,14,00,00),datetime('now')]);
To fix the problem I would suggest to change the datetime after the 'TimeStamps' to something that reflects the appropriate timestamps of the thingspeak data. So, I would utilize the timestamp variable created above. Try this,
lastTime = length(timestamps);
thingSpeakWrite (ChannelID,analyzedData,'WriteKey',writeAPIKey,'Fields',1,'timestamps',[timestamp(1),timestamp(lastTime)];
  4 Comments
Mario Chiappelli
Mario Chiappelli on 25 Jul 2019
Do what Walter said and change the 'DateRange' option to 'timestamps'
That was my bad, before you were sending date time values and now you are sending timestamp values. They are the same concept just different input formats.
I changed my code above.
Walter Roberson
Walter Roberson on 26 Jul 2019
[data,timestamps] = thingSpeakRead(681431,'ReadKey',readAPIKey,'Fields',4,'DateRange',[datetime(2019,7,6,14,00,00),datetime('now')]);
%% Analyze Data %%
% Add code in this section to analyze data and store the result in the
% 'analyzedData' variable. This assumes that the number of output rows
% is the same as the number of input rows
analyzedData = data;
thingSpeakWrite (706490,analyzedData,'WriteKey',writeAPIKey,'Fields',1,'Timestamps',timestamps);
If the number of output rows is not the same as the number of input rows, then you need to figure out what meaningful timestamps would be, taking into account that the timestamps must be unique for the channel.

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!