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 Center および File ExchangeWeather and Atmospheric Science についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by