How to extract the data from webread output?

25 views (last 30 days)
Could someone please help me with understanding how to extract the data from webread output?
The question is as follows:
I am loading the data from the server, and need to be able to ready it in Matlab as a table or mat file. The problem is that it is either a string from json code or a struct with many layers, and I do not quite understand how to extract variables that are linked together.
The code is a follows:
----
key = 'Q.N.DE.W2.S11.S13.N.A.LE.F4.T._Z.XDC._T.S.V.N._T'
url = [api 'data/QSA/' key];
options2 = weboptions('ContentType','text');
data2 = webread(url)%,options)
data1= webread(url,options2)
---
And I would like to be able to extract: these two linked variables:
<generic:ObsDimension value="2021-Q1"/>
<generic:ObsValue value="7854"/>
The first one-- as a string and the second one -- as a number.

Accepted Answer

Ive J
Ive J on 21 Jul 2021
Edited: Ive J on 21 Jul 2021
It's always better to first check the RESTful API documentations. In your case, you can get those values either by applying regexp to XML contents, or read/decode it in JSON format:
key = 'Q.N.DE.W2.S11.S13.N.A.LE.F4.T._Z.XDC._T.S.V.N._T';
api = 'https://sdw-wsrest.ecb.europa.eu/service/';
url = [api 'data/QSA/' key];
raw = webread(url);
data = jsondecode(raw);
% get ids/values
ObsDimension = {data.structure.dimensions.observation.values.id}.';
ObsValue = struct2table(data.dataSets.series.x0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0.observations); % ObsValue are stored here
% create a new table from these 2 arrays
res = table(ObsDimension, ObsValue{1, :}.', 'VariableNames', {'ObsDimension', 'ObsValue'})
res = 89×2 table
ObsDimension ObsValue ____________ ________ {'1999-Q1'} 0 {'1999-Q2'} 0 {'1999-Q3'} 0 {'1999-Q4'} 0 {'2000-Q1'} 991 {'2000-Q2'} 1042 {'2000-Q3'} 1093 {'2000-Q4'} 1143 {'2001-Q1'} 1237 {'2001-Q2'} 1330 {'2001-Q3'} 1424 {'2001-Q4'} 1518 {'2002-Q1'} 1679 {'2002-Q2'} 1840 {'2002-Q3'} 2001 {'2002-Q4'} 2162
You could also use regexp:
key = 'Q.N.DE.W2.S11.S13.N.A.LE.F4.T._Z.XDC._T.S.V.N._T';
api = 'https://sdw-wsrest.ecb.europa.eu/service/';
url = [api 'data/QSA/' key];
opts = weboptions('ContentType', 'text');
raw = webread(url, opts);
ObsDimensionNew = regexp(raw, '(?<=<generic:ObsDimension value=")(.*?)[^"/>]*', 'match').';
ObsValueNew = regexp(raw, '(?<=<generic:ObsValue value=")(.*?)[^"/>]*', 'match').';
resNew = table(ObsDimensionNew, ObsValueNew, 'VariableNames', {'ObsDimension', 'ObsValue'})
resNew = 89×2 table
ObsDimension ObsValue ____________ ________ {'1999-Q1'} {'0' } {'1999-Q2'} {'0' } {'1999-Q3'} {'0' } {'1999-Q4'} {'0' } {'2000-Q1'} {'991' } {'2000-Q2'} {'1042'} {'2000-Q3'} {'1093'} {'2000-Q4'} {'1143'} {'2001-Q1'} {'1237'} {'2001-Q2'} {'1330'} {'2001-Q3'} {'1424'} {'2001-Q4'} {'1518'} {'2002-Q1'} {'1679'} {'2002-Q2'} {'1840'} {'2002-Q3'} {'2001'} {'2002-Q4'} {'2162'}
  3 Comments
Peter Perkins
Peter Perkins on 27 Jul 2021
Even better, create a timetable, something like
>> ObsDimension = datetime(ObsDimension,'Format','uuuu-QQQ');
>> res = timetable(ObsValue{1, :}.', 'RowTimes',ObsDimension, 'VariableNames',{'ObsValue'});
>> head(res)
ans =
8×1 timetable
Time ObsValue
_______ ________
1999-Q1 0
1999-Q2 0
1999-Q3 0
1999-Q4 0
2000-Q1 991
2000-Q2 1042
2000-Q3 1093
2000-Q4 1143

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!