How to extract the data from webread output?

조회 수: 54 (최근 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 CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by