HOW DO I USE FOR LOOP TO GET VALUES IN MY ARRAY
이전 댓글 표시
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
2020년 11월 24일
"HOW WOULD I GET THE VALUES FOR EACH CELL ARRAY INTO EACH OF "fn" TITLES?"
fn{1},fn{2},.... ??
or
for i=1:length(fn)
fn{i}
end
Joseph Alberto
2020년 11월 24일
KALYAN ACHARJYA
2020년 11월 24일
TO GET VALUES
Which variables?
Joseph Alberto
2020년 11월 24일
답변 (1개)
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{:}];
카테고리
도움말 센터 및 File Exchange에서 Weather and Atmospheric Science에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!