How to utilize an existing API to download weather data and use it for calculations in a MATLAB script?

25 vues (au cours des 30 derniers jours)
I want to create a script that can download files from the National Solar Radiation Database using their API (I have an API Key) and convert them to tables, cells, or structures in the workspace so that I can use the data for calculations in MATLAB. (I am designing a solar power system, and want the irradiance data to create an accurate estimate of how much power I can expect specific panels to produce in that location. The data also includes things like temperature, windpseed etc. which are extremely useful for designing the system). The only purpose of this question is to figure out how to pull the data using the API and store it in a structure, cell array, or table.
This link shows an example of the API, and it does achieve my goal of entering coordinates to recieve weather data from the nearest station. I've read some documentation on the "webread" function, but I'm still shaky on how to achieve this in matlab. The only output formats from the API are JSON and XML. I imagine that XLM could be read with "xlmread" function according to the documentation, but I haven't worked with API's enough to set up an example and verify. I believe I have every toolbox for the R2020a release.

Réponse acceptée

millercommamatt le 8 Sep 2022
Modifié(e) : millercommamatt le 8 Sep 2022
Build a string that is structured like this and use it as the input to webread:
Use the information at the link you provided to specify the input variables you want.
%% INPUTS (all are strings/char)
api_key = 'DEMO_KEY';
lat = '40';
lon = '-150';
radius = '32';
%% fetch the data
api_base = '';
data_out_struct = webread(...
[ api_base '&api_key=' api_key '&lat=' lat '&lon=' lon '&radius=' radius]...
); % you'll get a struct out

Plus de réponses (1)

Vaishak le 25 Jan 2024
Below code is working for me, may be used as a reference:
% Parameters
Latitude = 40.7128; % Replace with actual latitude
Longitude = -74.0060; % Replace with actual longitude
year = '2019';
api_key = 'ENTER YOUR API CODE';
attributes = 'air_temperature,ghi,dhi';
leap_year = 'false';
interval = '15';
utc = 'false';
your_name = 'YOUR NAME';
reason_for_use = 'testing';
your_affiliation = 'researcher';
your_email = 'ENTER YOUR EMAIL';
mailing_list = 'true';
% Construct API URL
url = sprintf('', Longitude, Latitude, year, leap_year, interval, utc, your_name, your_email, your_affiliation, mailing_list, reason_for_use, api_key, attributes);
% Download and read CSV file
maxAttempts = 10;
attempt = 1;
while attempt <= maxAttempts
% Download CSV file
tempFolderName = tempname;
outputFilePath = fullfile(tempFolderName, 'downloaded_data.csv');
websave(outputFilePath, url);
% Read the downloaded CSV file
data = readtable(outputFilePath);
% Display or process the data as needed
disp('File content:');
% Successful download and read
disp('CSV file downloaded and read successfully.');
catch ME
fprintf('Error during download or CSV file read: %s\n', ME.message);
% Retry if attempts left
attempt = attempt + 1;
if attempt <= maxAttempts
error('Maximum number of download attempts reached. Unable to download the file.');


En savoir plus sur Downloads dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by