October 28, 2016
I'm finding the reference for thingSpeak extremely lacking so I have turned here in exasperation.
I am using the thingSpeakRead() function to read the most recent value from Field 1 of my private channel. The data that this field stores is a string of comma-separated values.
I want to take this list of values, parse them, and write them to their corresponding field on a different private channel I own.
I have the following code:
data = thingSpeakRead(readChannelID,'ReadKey', readAPIKey,'NumPoints', 1, 'Fields', );
dataArray = strsplit(data, ',');
If I try and run this code, I get the following error message in the "Output" box:
Error using strsplit
First input must be either a character vector or a string scalar.
So, obviously the data that thingSpeakRead() returned was not a string (or "character vector" as I guess MATLAB calls it...).
Not sure where to go from here.... is there a way to access this string of values stored in Field 1?
August 25, 2015
thingSpeakRead() by default returns data back as a numeric array. For your case, since the data on the ThingSpeak channel is a string, you should use the 'OutputFormat' names parameter to request for data back as a table:
data = thingSpeakRead(readChannelID,'ReadKey', readAPIKey,'NumPoints', 1, 'Fields', , 'OutputFormat', 'table');
You will then need to access the data in the table (data.<columnName>) to apply strsplit() function. Refer to the following link on how to access data in a table:
If this doesn't work, share the values stored in the data table and we might be able to provide additional suggestions.
October 28, 2016
Thanks for the reply! Clearly I have a lot to learn about MATLAB...
I now have the thingSpeakRead() function returning a table. It would appear that it thinks the column with the comma-delimited strings in it is a variable of type double
Here is my code:
%% Read Data %%
data = thingSpeakRead(readChannelID,'ReadKey', readAPIKey, 'OutputFormat', 'table');
%% Analyze Data %%
% Add code in this section to analyze data and store the result in the
% analyzedData variable.
testVar = data.allData(1);
.... and the output:
Timestamps: 1×1 datetime
min 01-Nov-2016 13:56:35
median 01-Nov-2016 13:56:35
max 01-Nov-2016 13:56:35
allData: 1×1 double
Name Size Bytes Class Attributes
data 1x2 1198 table
metacl 1x1 8 meta.class
readAPIKey 1x16 32 char
readChannelID 1x1 8 double
testVar 1x1 8 double
writeAPIKey 1x16 32 char
writeChannelID 1x1 8 double
Any idea why I am getting a double instead of a string?
Here is the first few rows of the table, with headers:
|30-10-16 13:14||135||1477844100, 11.13, 0.78, 8.73, 2.14, 11.64, 1.27, 14.74, 3.56, 21.95, 5.41, 0.00, 0.00, 5.01, 1.22, 1.08, 0.25,|
|30-10-16 13:29||136||1477845000, 11.15, 0.84, 9.39, 2.33, 11.66, 1.29, 15.03, 3.69, 22.85, 5.66, 0.00, 0.00, 5.02, 1.23, 1.07, 0.26,|
August 25, 2015
Thanks for sharing the additional info. ThingSpeakRead at present doesn't support the case where a numeric vector is stored in a single field. We will consider this in a future update of the ThingSpeakRead function.
In the meanwhile, to access your data in a MATLAB Analysis App, you can use the following lines of code instead:
data = webread('https://api.thingspeak.com/channels/<channelID>/feeds.json?api_key=<read_Key>&results=1')
myData = data.feeds.field1;
The data returned from thingSpeak will be stored in the variable data as a structure. As shown in your previous post, since the value of interest is stored in field1, myData should contain the data of interest.
NOTE: you need to replace <channelID> and <read_Key> with their appropriate values.
Let me know if this doesn't work for you.
Most Users Ever Online: 114
Currently Browsing this Page:
Guest Posters: 1
Newest Members:celinabw2, FitoSprayscroma, LunnonGor, lancevl16, bernicenk60, veronicadq18
Administrators: Hans: 387, lee: 457