Open OpenWeatherMap API in matlab

Hi,
I just need to know the weather at a current position.
I have the following script, but it doesn't work.
key = 'MyKey';
op = 'TA2';
z = '1';
x = '51.36';
y = '6.16';
options = weboptions('ContentType','json');
url = ['http://maps.openweathermap.org/maps/2.0/weather/TA2/',z,'/',x,'/',y,'?date=1527811200&opacity=0.9&fill_bound=true&appid=',key];
Current_Data = webread(url, options);
Can anyone tell me what is going wrong here? I got the folowing URL from OpenWeatherMap:
http://maps.openweathermap.org/maps/2.0/weather/{op}/{z}/{x}/{y}?appid={API key}
Thanks in advance

Antworten (1)

Yongjian Feng
Yongjian Feng am 22 Jul. 2021

0 Stimmen

What does it mean by "it doesn't work"? Any error message you saw?
Try the html directly outside matlab and make sure it works first. You can use curl from command line window.

3 Kommentare

Dion Theunissen
Dion Theunissen am 22 Jul. 2021
The URL is not working, that's the issue, but I cant see the mistake..
https://openweathermap.org/api/weather-map-2#data
Yongjian Feng
Yongjian Feng am 22 Jul. 2021
Normally we can use these steps to narrow down the issue:
  1. Use curl command outside matlab to make sure the URL works. If so
  2. Hard code this URL in matlab and make sure it works
  3. Use substitution like your example
Yongjian Feng
Yongjian Feng am 24 Jul. 2021
@Dion Theunissen: When you run webread(url) from matlab command line, what error does it shown on the command line window? I tried your example, it shows HTTP status 401, which means unauthorized. This is correct because I don't have an API key. But your url looks good.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Weather and Atmospheric Science finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021a

Gefragt:

am 22 Jul. 2021

Kommentiert:

am 24 Jul. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by