HOW DO I USE FOR LOOP TO GET VALUES IN MY ARRAY
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Mohammad Sami
on 24 Nov 2020
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{:}];
0 Comments
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!