Can Matlab analysis plugin parse json or xml? | 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 Matlab analysis plugin parse json or xml?
No permission to create posts
September 15, 2015
1:14 am
Avatar
rw950431

Top
Members
Forum Posts: 271
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

I know nothing about Matlab so pardon the newbie question..

Does the thingspeak implementation contain modules to parse JSON and XML? Alternatively is it possible to include functions like https://www.mathworks.com/matlabcentral/fileexchange/20565-json-parser/content/parse_json.m

September 15, 2015
11:21 am
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 47
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

There's an arbitrary XML reader function:
Here's info about it -- http://www.mathworks.com/help/matlab/import_export/importing-xml-documents.html

There's no arbitrary JSON decoder in MATLAB, but if you get JSON data back from webread, it is parsed into a structure. Typically, this is what people want to do.

api = 'http://climatedataapi.worldbank.org/climateweb/rest/v1/';
url = [api 'country/cru/tas/year/USA'];
S = webread(url)

S =

112x1 struct array with fields:

year
data

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

September 15, 2015
9:52 pm
Avatar
rw950431

Top
Members
Forum Posts: 271
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Awesome news Rob!

Do you happen to know the MATLAB syntax to access a particular element of the structure returned by webread?

The example you used returns the array of structures shown below: how would I get the value for the last year available? (Like I said I'm a total newbie at MATLAB)

[{"year":1901,"data":6.6187487},
{"year":1902,"data":6.4643273},
{"year":1903,"data":6.073844},
{"year":1904,"data":6.149883},
....
{"year":2012,"data":7.939524}]

September 16, 2015
2:05 pm
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 47
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

If you're sure the last element in S contains the latest year (which it does in this case), you can just do:
S(end).data

If you're not sure which item has the greatest year in it, you need to do two steps:
[maxyear,idx] = max([S.year]) % Figure out the element with the largest year and put the index into idx
S(idx).data

For a more complete tutorial on MATLAB indexing, see:
http://www.mathworks.com/help/matlab/indexing.html

And, if you want something deeper:
https://www.coursera.org/courses?query=matlab

-Rob

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

September 17, 2015
11:22 pm
Avatar
rw950431

Top
Members
Forum Posts: 271
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Thanks for that, its very helpful.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
43 Guest(s)

Currently Browsing this Page:
1 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:

gwenbk2, eloisedq3, AEtedesar, RichardWosus, Kellynapse, tejomaya

Administrators: Hans: 387, lee: 457