How to extract the data from webread output?

29 ビュー (過去 30 日間)
Veronika Stolbova
Veronika Stolbova 2021 年 7 月 21 日
コメント済み: Veronika Stolbova 2021 年 7 月 28 日
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.

採用された回答

Ive J
Ive J 2021 年 7 月 21 日
編集済み: Ive J 2021 年 7 月 21 日
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 件のコメント
Peter Perkins
Peter Perkins 2021 年 7 月 27 日
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
Veronika Stolbova
Veronika Stolbova 2021 年 7 月 28 日
Thanks a lot!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJSON Format についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by