Can't use data from thingSpeakRead to parse comma delimited values | ThingSpeak Apps | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Can't use data from thingSpeakRead to parse comma delimited values
No permission to create posts
October 28, 2016
3:27 pm
Avatar
jaza_tom
New Member
Members
Forum Posts: 3
Member Since:
October 28, 2016
sp_UserOfflineSmall Offline

Hello,

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', [1]);

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?Cry

October 28, 2016
3:46 pm
Avatar
Adarsh_Murthy

MathWorks
Members
Forum Posts: 62
Member Since:
August 25, 2015
sp_UserOfflineSmall Offline

Hi, 

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', [1], '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:

https://www.mathworks.com/help/matlab/ref/table.html

https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html

 

If this doesn't work, share the values stored in the data table and we might be able to provide additional suggestions.

 

HTH!

November 1, 2016
2:45 pm
Avatar
jaza_tom
New Member
Members
Forum Posts: 3
Member Since:
October 28, 2016
sp_UserOfflineSmall Offline

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.
summary(data);

disp ----------------;

testVar = data.allData(1);
disp(testVar);

disp ----------------;

whos;

.... and the output:

Variables:

Timestamps: 1×1 datetime
Values:

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
Values:

min NaN
median NaN
max NaN
NaNs 1

----------------
NaN

----------------
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?Yell

EDIT:

Here is the first few rows of the table, with headers:

created_at entry_id field1
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,
November 2, 2016
12:33 pm
Avatar
Adarsh_Murthy

MathWorks
Members
Forum Posts: 62
Member Since:
August 25, 2015
sp_UserOfflineSmall Offline

Hi, 

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.

January 18, 2017
12:49 pm
Avatar
mohamedali1993
Bethesda, MD
New Member
Members
Forum Posts: 4
Member Since:
December 13, 2016
sp_UserOfflineSmall Offline

Adarsh_Murthy, what you recommended works perfectly!

Thank you

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
19 Guest(s)

Currently Browsing this Page:
2 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

celinabw2, FitoSprayscroma, LunnonGor, lancevl16, bernicenk60, veronicadq18

Administrators: Hans: 387, lee: 457