urlread function: put variable in string

2 visualizaciones (últimos 30 días)
x y
x y el 3 de Sept. de 2015
Editada: Cedric el 3 de Sept. de 2015
I am using the urlread() function to read in information from an url. How could I include variables into the url address?
I have latitude and longitude information which I would like to implement into the url when looping through the data. Here some example for lat and long variables:
lat=43.2;
long=116.3
I tried using num2str(variablename) but this failed with an unexpected matlab error
alt= urlread('http://api.geonames.org/astergdem?lat=' num2string(lat) '&lng=' num2str(long) '&username=test12345&style=full&type=JSON')

Respuesta aceptada

Cedric
Cedric el 3 de Sept. de 2015
Editada: Cedric el 3 de Sept. de 2015
You can concatenate strings using square brackets, but I usually prefer building a final string using SPRINTF, e.g.
url = sprintf( 'http://api.geonames.org/astergdem?lat=%f&lng=%f&username=test12345&style=full&type=JSON', lat, long ) ;
data = urlread( url ) ;
The first string in the call to SPRINTF is called formatSpec. Look in MATLAB doc and you will see what the two %f stand for and how to tailor them to your needs. In your command window, type
doc sprintf
and look for the formatSpec.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by