Structure data obtained from webread
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jorge Luis Paredes Estacio
el 17 de Jul. de 2024
Comentada: Jorge Luis Paredes Estacio
el 17 de Jul. de 2024
Hi, I would like to have to automatize a procedure to extract data from different URLs, One example of one URL link is the following:
% Generating the URL link
url='https://earthquake.usgs.gov/fdsnws/event/1/query?format=quakeml&eventid=us6000n8tq';
% Reading the data
data=webread(url);
The variable “data” contains huge amount of information that I would like to structure in order to access and retrieve data easily without the need of creating a xml file.
I would appreciate the help.
0 comentarios
Respuesta aceptada
Vandit
el 17 de Jul. de 2024
Hi Jorge,
To automate the extraction and structuring of data from URLs like the one provided, the "webread" function in MATLAB can be used to read the data. Subsequently, the "xml2struct" function can convert the XML data into a MATLAB structure for easy access, eliminating the need to create an XML file. Below is the code snippet for reference that reads the data, parses the XML content, and converts it to a MATLAB structure for easy access:
% Generating the URL link
url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=quakeml&eventid=us6000n8tq';
% Reading the data
data = webread(url);
% Parsing the XML content
xmlData = java.io.StringReader(data);
xmlInputFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
xmlBuilder = xmlInputFactory.newDocumentBuilder();
xmlDoc = xmlBuilder.parse(org.xml.sax.InputSource(xmlData));
% Converting XML to a MATLAB structure for easy access
xmlStruct = xml2struct(xmlDoc);
% Example access to specific data
eventDescription = xmlStruct.q_colon_quakeml.eventParameters.event.description{1, 1}.text.Text;
disp('Event Description:');
disp(eventDescription);
In the above code snippet, the "xml2struct" function is used to convert the XML data stored in the 'data' variable into a structured MATLAB variable 'xmlStruct'. Specific information from the 'xmlStruct' variable can be accessed and retrieved using dot notation or indexing, depending on the structure of the XML data.
However, please note that the specific structure of the XML data may vary depending on the URL and the data source. It may be necessary to inspect the structure of the 'xmlStruct' variable to determine the appropriate syntax for accessing the desired information, such as the event description in the above code snippet.
Additionally, the "xml2struct" function is not a built-in function in MATLAB. If this function is not available, it can be downloaded from MATLAB File Exchange using the following link:
Hope this helps.
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!