MATLAB: Urlread function: put variable in string

num2stringurl

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')

Best Answer

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.