urlread function: put variable in string

3 Ansichten (letzte 30 Tage)
x y
x y am 3 Sep. 2015
Bearbeitet: Cedric am 3 Sep. 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')

Akzeptierte Antwort

Cedric
Cedric am 3 Sep. 2015
Bearbeitet: Cedric am 3 Sep. 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.

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by