HOW DO I USE FOR LOOP TO GET VALUES IN MY ARRAY

조회 수: 10 (최근 30일)
Joseph Alberto
Joseph Alberto 2020년 11월 24일
답변: Mohammad Sami 2020년 11월 24일
key = 'key';
options = weboptions('ContentType','json');
url = ['https://api.openweathermap.org/data/2.5/weather?q=San Francisco&units=imperial',...
'San Francisco','&APPID=',key];
data = webread(url,options);
fn = fieldnames(data);
for k=1:length(fn)
try
forecast = data.(fn{k})
end
end
RETURNS:
forecast =
struct with fields:
lon: -122.4200
lat: 37.7700
forecast =
struct with fields:
id: 801
main: 'Clouds'
description: 'few clouds'
icon: '02n'
forecast =
'stations'
forecast =
struct with fields:
temp: 283.4000
feels_like: 278.2900
temp_min: 282.0400
temp_max: 284.8200
pressure: 1018
humidity: 87
forecast =
10000
forecast =
struct with fields:
speed: 6.7000
deg: 280
forecast =
struct with fields:
all: 20
forecast =
1.6062e+09
forecast =
struct with fields:
type: 1
id: 5817
country: 'US'
sunrise: 1.6061e+09
sunset: 1.6062e+09
forecast =
-28800
forecast =
5391959
forecast =
'San Francisco'
forecast =
200
fn =
13×1 cell array
{'coord' }
{'weather' }
{'base' }
{'main' }
{'visibility'}
{'wind' }
{'clouds' }
{'dt' }
{'sys' }
{'timezone' }
{'id' }
{'name' }
{'cod' }
IM LOOKING TO PUT THIS INTO A TABLE
EX. COORD: lon: -122.420 lat: 37.7700
  댓글 수: 4
KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 11월 24일
TO GET VALUES
Which variables?
Joseph Alberto
Joseph Alberto 2020년 11월 24일
coord = lon: -122.4200, lat: 37.7700
weather = id: 801
main: 'Clouds'
description: 'few clouds'
icon: '02n'
and so forth...

댓글을 달려면 로그인하십시오.

답변 (1개)

Mohammad Sami
Mohammad Sami 2020년 11월 24일
You can try the following to unnest your data into a table
key = 'key';
options = weboptions('ContentType','json');
url = ['https://api.openweathermap.org/data/2.5/weather?q=San Francisco&units=imperial',...
'San Francisco','&APPID=',key];
a = webread(url,options);
a = struct2table(a);
i = varfun(@isstruct,a,'OutputFormat',"uniform");
b = varfun(@struct2table,a(:,i),"OutputFormat","cell");
b = cellfun(@(x,y)renamevars(x,x.Properties.VariableNames,strcat(y,'_',x.Properties.VariableNames)),b,a.Properties.VariableNames(i),'UniformOutput',false);
c = [a(:,~i) b{:}];

카테고리

Help CenterFile Exchange에서 Weather and Atmospheric Science에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by