read Information from a web

9 views (last 30 days)
flashpode
flashpode on 28 Sep 2021
Answered: Sameer on 15 Feb 2024
Hi, I got this code to run all the information from a page but when I run it on matlab I get errors. The code is:
"descripcion" : "exito",
"estado" : 200,
"datos" = "https://opendata.aemet.es/opendata/sh/79adac19",
"metadatos" : "https://opendata.aemet.es/opendata/sh/b3aa9d28"
in the first link I got all the information I want how can I get it??
  2 Comments
Image Analyst
Image Analyst on 28 Sep 2021
Where did you get that code? It's not MATLAB code. Just putting that into a script will not cause it to "read" or "run" "all the information from a page". You probably need to call webread() or something. Please attach your script (m-file) if you have one.
flashpode
flashpode on 28 Sep 2021
I just got this from the page and do not know how to do it to be able to read it

Sign in to comment.

Answers (1)

Sameer
Sameer on 15 Feb 2024
Hi,
From my understanding, you're looking to retrieve data from a web service, and you've shared a JSON snippet that contains a URL under the datos key, which seems to be the endpoint where the data can be accessed.
To retrieve data from a web service using MATLAB, you can utilize the "webread" function.
Please refer to the documentation links below:
Below is an example MATLAB code to accomplish this:
% Defined the URL from the "datos" field
datosUrl = 'https://opendata.aemet.es/opendata/sh/79adac19';
% If an API key is required, setup using weboptions
% Replace 'your_api_key_here' with the actual API key if needed
options = weboptions('HeaderFields', {'api_key', 'your_api_key_here'});
%If an API key is not required, you can simplify the options setup
%options = weboptions();
% Retrieve the data from the URL
try
% Use webread to get the data
data = webread(datosUrl, options);
% Display or process the data
disp(data);
catch ME
% Handle any errors that may occur
fprintf('Error retrieving data: %s\n', ME.message);
end
I hope this helps!
Sameer

Community Treasure Hunt

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

Start Hunting!